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)
);
复制代码
相关文章
相关标签/搜索