过滤:
取list单属性:
list转map:
set去重:
distinct去重:
List groups = list.stream().distinct().collect(Collectors.toList());
根据对象某一个属性去重:
List list = new ArrayList();
list.add(new User(“aaa”, 1));
list.add(new User(“bbb”, 2));
list.add(new User(“aaa”, 3));
list.add(new User(“ccc”, 4));
ArrayList list2 = list.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));
list2.forEach(System.out::println);
输出结果:
User [name=aaa, age=1]
User [name=bbb, age=2]
User [name=ccc, age=4]