JDK 8 List指定對象去重

創建對象

 Student student1 = new Student("張三",20,"周");
        Student student2 = new Student("李四",30,"上號");
        Student student3 = new Student("王五",18,"阿西吧");
        Student student4 = new Student("氨綸",19,"阿西吧");
        Student student5 = new Student("氨綸",19,"阿西吧1");

其中對於name去重

 List<Student> collect = Arrays.asList(student1,student2,student3,student4,student5);
 List<Student> unique = collect.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));
        unique.forEach(p -> System.out.println(p));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章