java8 流式根据集合中的某个属性,对集合做去重处理

示例:
Person p1 = new Person(1, “11”);
Person p2 = new Person(1, “22”);
Person p3 = new Person(2, “11”);
List persons = Arrays.asList(p1,p2,p3);
ArrayList unique = persons.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Optional.ofNullable( Person::getName) ))),
ArrayList::new)
);
unique.forEach(p -> System.out.println§);

你可能感兴趣的:(java8 流式根据集合中的某个属性,对集合做去重处理)