public interface ListUtilsHook<T> {
public boolean find(T t);
}
public class ListUtils {
/**
* List过滤出符合条件的元素
* @param <T>
* @param list
* @param listUtilsHook
* @return
*/
public static <T> List<T> filter(List<T> list, ListUtilsHook<T> listUtilsHook) {
ArrayList<T> result = new ArrayList<T>();
for (T t : list) {
if (listUtilsHook.find(t)) {
result.add(t);
}
}
result.trimToSize();
return result;
}
}
List<Entity> result = ListUtils.filter(数据源, new ListUtilsHook<Entity>(){
@Override
public boolean find(Entity entity) {
//eg: return entity.getId().equals("###");
return 筛选条件;
}
});