1 public static void main(String[] args) { 2 EmployDO zhangsan = new EmployDO.Builder().name("张三").sex("男").position("职员").status("在职").age("20").build(); 3 EmployDO lisi = new EmployDO.Builder().name("李四").sex("男").position("职员").status("在职").age("24").build(); 4 EmployDO wangwu = new EmployDO.Builder().name("王五").sex("男").position("职员").status("离职").age("21").build(); 5 EmployDO yangliu = new EmployDO.Builder().name("杨六").sex("男").position("职员").status("在职").age("25").build(); 6 EmployDO lihua = new EmployDO.Builder().name("李华").sex("女").position("领导").status("在职").age("20").build(); 7 EmployDO xiaohua = new EmployDO.Builder().name("小花").sex("女").position("职员").status("离职").age("23").build(); 8 EmployDO xiaofeng = new EmployDO.Builder().name("小凤").sex("女").position("领导").status("在职").age("25").build(); 9 List<EmployDO> employDOS = Arrays.asList(zhangsan, lisi, wangwu, yangliu, lihua, xiaohua, xiaofeng); 10 // partitioningBy可以根据判断条件将一个集合拆分为两个集合, 返回值为map,其中的key为布尔值 11 Map<Boolean, List<EmployDO>> map = employDOS.stream().collect(Collectors.partitioningBy(employDO -> "在职".equals(employDO.getStatus()))); 12 System.out.println("在职员工:" + JSON.toJSONString(map.get(Boolean.TRUE))); 13 System.out.println("离职员工:" + JSON.toJSONString(map.get(Boolean.FALSE))); 14 }
输出结果:
在职员工:
[{"age":"20","name":"张三","position":"职员","sex":"男","status":"在职"},{"age":"24","name":"李四","position":"职员","sex":"男","status":"在职"},{"age":"25","name":"杨六","position":"职员","sex":"男","status":"在职"},{"age":"20","name":"李华","position":"领导","sex":"女","status":"在职"},{"age":"25","name":"小凤","position":"领导","sex":"女","status":"在职"}]
离职员工:
[{"age":"21","name":"王五","position":"职员","sex":"男","status":"离 职"},{"age":"23","name":"小花","position":"职员","sex":"女","status":"离职"}]