Java8中Stream流自定义key去重

题目:一个学生集合,假设姓名和生日相同,就算是同一个人,需将其过滤,如何实现?

import java.util.*;
import java.util.stream.Collectors;

public class StudentMethod {
    public static void main(String[] args) {
        Student student1 = new Student(1, "张三", "1991-01", 27);
        Student student2 = new Student(2, "李四", "1992-01", 15);
        Student student3 = new Student(3, "王五", "1993-01", 24);
        Student student4 = new Student(4, "赵六", "1994-01", 15);
        Student student5 = new Student(5, "李七", "1995-01", 29);
        Student student6 = new Student(6, "钱八", "1996-01", 18);
        Student student7 = new Student(7, "孙九", "1997-01", 25);
        Student student8 = new Student(8, "周八", "1998-01", 19);
        Student student9 = new Student(8, "周八", "1998-01", 19);

        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        list.add(student6);
        list.add(student7);
        list.add(student8);
        list.add(student9);

        /**
         * 第一种双层for循环形式
         */
        long start1 = System.currentTimeMillis();
        // 新的集合用来存放最后的去重后的集合
        List<Student> listNew = new ArrayList<>();
        for (Student s1 : list) {
            // 用于循环遍历的时候,不同的时候一直为true,相同的时候则为false
            boolean flag = true;
            for (Student s2 : listNew) {
                if (s1.getName().equals(s2.getName()) && s1.getBirth().equals(s2.getBirth())) {
                    // 相同的时候则跳出当前循环,说明存在姓名与生日相同的人
                    flag = false;
                    break;
                }
            }
            if (flag) {
                listNew.add(s1);
            }
        }
        System.out.println("==================第一种双层for循环形式==================");
        // 按ID排序
        Collections.sort(listNew, Comparator.comparing(Student::getId));
        listNew.forEach(m1 -> {
            System.out.println(m1.toString());
        });
        long end1 = System.currentTimeMillis();
        System.out.println("第一种所有时间=" + (end1 - start1));

        /**
         * 第二种流的形式
         */
        long start2 = System.currentTimeMillis();
        // List转map:把list里面的元素拿出来作为map的value,自己定义key
        // a->a, (k1,k2)->k1: 如果有重复的key就返回第一个
        // m1.getName() + m1.getBirth()是自定义的key
        Map<String, Student> map1 = list.stream().collect(Collectors.toMap(m1 -> m1.getName() + m1.getBirth(), a -> a, (k1, k2) -> k1));
        // map 转 list
        List<Student> collect = map1.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList());
        // 按ID排序
        Collections.sort(collect, Comparator.comparing(Student::getId));
        System.out.println("==================第二种流的形式==================");
        collect.forEach(m1 -> {
            System.out.println(m1.toString());
        });
        long end2 = System.currentTimeMillis();
        System.out.println("第二种所有时间=" + (end2 - start2));
    }
}

class Student {
    private int id;
    private String name;
    private String birth;
    private int age;

    public Student() {

    }

    public Student(int id, String name, String birth, int age) {
        this.id = id;
        this.name = name;
        this.birth = birth;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirth() {
        return birth;
    }

    public void setClassNo(String classNo) {
        this.birth = birth;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birth='" + birth + '\'' +
                ", age=" + age +
                '}';
    }
}

结果:

 

posted @ 2022-04-28 16:29  蛋挞小子  阅读(1036)  评论(0编辑  收藏  举报