How do I create two different compliementary lists using same input

前端 未结 2 1087
轮回少年
轮回少年 2021-01-12 14:18

In my previous question - How to filter the age while grouping in map with list I was able to find the name to age groups using List users. Now I am

2条回答
  •  自闭症患者
    2021-01-12 15:18

    You're after the partitioningBy collector:

    Map> result = 
                 users.stream().collect(partitioningBy(u -> u.getAge() > 21));
    

    Then use it as follows:

    List userAboveThreshold = result.get(true);
    List userBelowThreshold = result.get(false);
    

提交回复
热议问题