1.定义一个 ListFilterStrategy 接口,实现过滤条件封装
/**
* @Auther: wuzheng
* @Date: 2018/12/5 14:01
* @Description: 定义List过滤基本方法
*/
public interface ListFilterStrategy<T> {
public boolean contain(T t);
}
2.ListUtils 实现
/**
* @Auther: wuzheng
* @Date: 2018/12/5 13:56
* @Description: List工具类,存放一些比较常用的公共部分功能
*/
public final class ListUtils {
/**
* 根据条件对List进行过滤
* @param list
* @param strategy
* @param <T>
* @return
*/
public static <T> List<T> filter(List<T> list, ListFilterStrategy<T> strategy) {
List<T> rList = new ArrayList<>();
if(CollectionUtils.isNotEmpty(list)){
for (int i = 0; i < list.size(); i++) {
T t = list.get(i);
if (strategy.contain(t)){
rList.add(t);
}
}
}
((ArrayList<T>) rList).trimToSize();//释放申请的多余的空间
return rList;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i <10; i++) {
list.add(i);
}
final List<Integer> alist = Arrays.asList(1, 2, 3);
List<Integer> rList=filter(list, new ListFilterStrategy<Integer>() {
@Override
public boolean contain(Integer integer) {
return alist.contains(integer);
}
});
System.out.println(rList.size());
}