JAVA8-Lambda-distinct(去重)

去除重复元素/重复对象

使用去重API

    //注意:Student对象需要重写equals方法和hashcode方法
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三", 19, "杭州"));
        list.add(new Student("张三", 19, "杭州"));
        list.add(new Student("李四", 18, "海口"));

        List<Student> studentList = list.stream().distinct().collect(Collectors.toList());
        System.out.println(studentList);
    }

结果:

posted @ 2022-11-05 23:19  CodeLuckly  阅读(1259)  评论(0编辑  收藏  举报