lambda對實體的某個屬性進行去重

實體類:bash

@Data
class People {
    private String name;
    private int age;
    private String detail;

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
複製代碼

經過lambda語法某個屬性去重:this

peopleList = peopleList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(People::getName))
                ), ArrayList::new)
);
複製代碼
相關文章
相關標籤/搜索