1.几种处理数据冗余,去重的方法
根据实体类id去重
List idList =Lists.newArrayList(); Listentity = idList.stream().distinct(). collect(Collectors.toList());
根据实体类指定字段去重,该实例中以formId为例
List idList =Lists.newArrayList(); ArrayListfilterList = idList.stream(). collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(Entity::getFormId))), ArrayList::new));
2.迭代器 Iterator
实例:集合除去指定元素{3,4,5,6,6,7,8,8} 除去6 and 8
List idList = Lists.newArrayList(3, 4, 5, 6, 6, 7, 8, 8); List filterList = Lists.newArrayList(6, 8); Iteratoriterator = idList.iterator(); while (iterator.hasNext()) { Integer next = iterator.next(); if (filterList.contains(next)) { iterator.remove(); } }
3.stream()流 : allMatch 和 noneMatch 的用法
allMatch: 所有的都成立, 返回 true;
anyMatch: 任意一个成立, 返回 true;
noneMatch: 所有的都不成立, 返回 true;
Integer number = 0;
Boolean b =IdList.stream().anyMatch(a->{
a.getId().equals(number) || a.getId()>0;
});
…



