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

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();
    }

欢迎关注我的微信公众号,更多内容将在微信公众号中发布:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值