package com.xiniunet.web.tool;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Created by xn on 15/7/22.
*/
public class ListUtil {
/**
* 判断集合中是否存在该对象的数据
* @param list
* @param value
* @param
* @return 返回在集合中首次匹配到的对象数据
*/
public static T matchObject(List list,T value) {
if(list == null || list.size() == 0 || value == null){
return null;
}
for(T t : list) {
if(value.equals(t)){
return t;
}
}
return null;
}
/**
* 判断集合中是否存在该长整型数值的数据对象
* @param list
* @param id
* @param
* @return 返回首次符合长整型数值的数据对象
*/
public static T matchObject(List list,Long id) {
if(list == null || list.size() == 0 || id == null){
return null;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
return t;
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 判断集合中是否存在该长整型数值的数据对象
* @param list
* @param id
* @param
* @return 返回符合长整型数值的数据集合对象
*/
public static List matchList(List list,Long id) {
List result = new ArrayList();
if(list == null || list.size() == 0 || id == null){
return result;
}
Class clazz = list.get(0).getClass();</