Java Stream: divide into two lists by boolean predicate

后端 未结 4 2062
温柔的废话
温柔的废话 2020-12-01 13:54

I have a list of employees. They have isActive boolean field. I would like to divide employees into two lists: activeEmployees

4条回答
  •  抹茶落季
    2020-12-01 14:26

    You can also use groupingBy in this case as there are 2 group posibilities (active and inactive employees):

    Map> grouped = employees.stream()
                    .collect(Collectors.groupingBy(Employee::isActive));
    
    List activeEmployees = grouped.get(true);
    List formerEmployees = grouped.get(false);
    

提交回复
热议问题