Java8 新特性 Stream流操作List集合 (二)

h_j_c_123發表於2020-11-04

lambda表示式的list去重和list轉map

    public static List<Employee> createEmployee() {
        List<Employee> employeeList = new ArrayList<Employee>();
        Employee employee1 = new Employee();
        Employee employee2 = new Employee();
        Employee employee3 = new Employee();

        employee1.setEmployeeId("1");
        employee1.setEmployeeName("張三");
        employee1.setEmployeeSex("男");

        employee2.setEmployeeId("2");
        employee2.setEmployeeName("王五");
        employee2.setEmployeeSex("男");

        employee3.setEmployeeId("3");
        employee3.setEmployeeName("李四");
        employee3.setEmployeeSex("男");

        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);

        return employeeList;
    }

    public static List<Employee> createEmployee2() {
        List<Employee> employeeList = new ArrayList<Employee>();
        Employee employee1 = new Employee();
        Employee employee2 = new Employee();
        Employee employee3 = new Employee();
        Employee employee4 = new Employee();

        employee1.setEmployeeId("1");
        employee1.setEmployeeName("張三");
        employee1.setEmployeeSex("男");

        employee2.setEmployeeId("1");
        employee2.setEmployeeName("王五");
        employee2.setEmployeeSex("男");

        employee1.setEmployeeId("1");
        employee1.setEmployeeName("田七");
        employee1.setEmployeeSex("男");

        employee3.setEmployeeId("3");
        employee3.setEmployeeName("李四");
        employee3.setEmployeeSex("男");

        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);

        return employeeList;
    }

    public static List<String> createString() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("1");
        list.add("2");
        return list;
    }

    public static void list() {
        // 去重list
        List<String> listString = createString();
        System.out.println(listString.stream().distinct().collect(Collectors.toList()));

        System.out.println("******************************************************************");

        // list 轉 map map的value 就是 Employee 物件
        List<Employee> employeeList = createEmployee();
        Map<String, Employee> map = employeeList.stream().collect(Collectors.toMap(Employee::getEmployeeId, employee -> employee));
        map.forEach((k, v) -> {
            System.out.println("key: " + k + "    value: " + v);
        });

        System.out.println("******************************************************************");

        // list 轉 map  涉及key 重複
        // 第一種寫法  (k1, k2) -> k1) 的意思是保留 k1 存在重複不傳第三個處理引數會報錯 Duplicate key
        List<Employee> employeeList2 = createEmployee2();
        Map<String, String> map1 = employeeList2.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> e.getEmployeeName(), (k1, k2) -> k1));
        System.out.println(map1);

        System.out.println("******************************************************************");

        // 第二種寫法
        Map map2 = employeeList2.stream().collect(HashMap::new, (map3, e) -> map3.put(e.getEmployeeId(), e.getEmployeeName()), Map::putAll);
        System.out.println(map2);

        System.out.println("******************************************************************");

        // 處理遍歷中存在null 的情況  value 為 null 會報空指標
        // 異常重現
//        Map<String, String> map3 = employeeList.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> e.getEmployeeBirthDay()));
//        System.out.println(map3);

        // 解決異常
        Map<String, String> map3 = employeeList.stream().collect(Collectors.toMap(e -> e.getEmployeeId(), e -> Optional.ofNullable(e.getEmployeeBirthDay()).orElse("沒有資料")));
        System.out.println(map3);

        System.out.println("******************************************************************");

        //  當 key 全部為 null 時 而 value 唯一就會報錯出現Duplicate key value的情況
        // 異常重現
//        Map<String, String> map4 = employeeList2.stream().collect(Collectors.toMap(Employee::getEmployeeBirthDay, Employee::getEmployeeName));
//        System.out.println(map4);

        // 解決異常 指定出現重複的key之後保留的欄位 (解決方法很多,中心圍繞在如何解決掉重複的問題,一般都是覆蓋)
        Map<String, String> map4 = employeeList2.stream().collect(Collectors.toMap(Employee::getEmployeeBirthDay, Employee::getEmployeeName, (k1, k2) -> k1));
        System.out.println(map4);

    }

    public static void main(String[] args) {
        list();
    }

 

相關文章