Created
October 7, 2013 14:00
-
-
Save seveniu/6868502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.Util; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: niu | |
* Date: 13-9-26 | |
* Time: 下午7:51 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class ListUtil<T> { | |
/** | |
* 在给定的List集合中,查询符合要求的对象,并将其添加到一个新的List集合中 | |
* 自定义数据类型需要重写 equals 方法 | |
* @param list 要查询的list | |
* @param str 对象的属性名,用“.”分开 | |
* @param value the value to compare | |
* @return 符合要求的List集合 | |
*/ | |
public List<T> hasContain(List<T> list, String str, Object value) { | |
if (list == null) | |
throw new RuntimeException("传入的 list 为 null"); | |
List<T> resultList; | |
resultList = new ArrayList<T>(); | |
if (list.size() == 0) | |
return resultList; | |
T obj; | |
Object result; | |
StringBuilder stringBuffer = new StringBuilder(); | |
String[] porpos = str.split("\\."); | |
System.out.println("解析得到的方法:"); | |
for (int i = 0 ; i < porpos.length ; i++) { | |
String first = porpos[i].substring(0,1); | |
stringBuffer.append("get"); | |
stringBuffer.append(first.toUpperCase()); | |
stringBuffer.append(porpos[i].substring(1)); | |
porpos[i] = stringBuffer.toString(); | |
stringBuffer.setLength(0); | |
System.out.println(porpos[i] + ", "); | |
} | |
for (T aList : list) { | |
obj = aList; | |
result = obj; | |
for (String porpo : porpos) { | |
result = invokeMethod(result, result.getClass(), porpo); | |
if (result == null) | |
return resultList; | |
} | |
System.out.println("遍历对象:" + aList + " 期望值:" + value + " 结果:" + result); | |
if (value.equals(result)) { | |
System.out.println("添加对象:" + aList); | |
resultList.add(obj); | |
} | |
} | |
System.out.println("共添加 " + resultList.size() + " 个对象"); | |
return resultList; | |
} | |
@SuppressWarnings("unchecked") | |
private Object invokeMethod(Object instanceObj, Class clazz, String methodName) { | |
Object result = null; | |
try { | |
System.out.println( instanceObj.getClass()); | |
System.out.println(methodName); | |
Method method = clazz.getMethod(methodName); | |
result = method.invoke(instanceObj); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
public List<T> addToList(T...obj) { | |
List<T> result = new ArrayList<T>(); | |
for (T t : obj) { | |
result.add(t); | |
} | |
return result; | |
} | |
public List<T> listAddList(List<T> base, List<T>...lists){ | |
for (List<T> list : lists) { | |
for (T t : list) { | |
base.add(t); | |
} | |
} | |
return base; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go go study....