Collectors.partitioningBy
功能说明:将一个list按照条件拆分为两个子list
功能代码实例
public static void main(String[] args) {
Artist artist = new Artist();
artist.setMembers(Arrays.asList("1", "2"));
ArrayList<Artist> artists = new ArrayList<>();
artists.add(artist);
Map<Boolean, List<Artist>> booleanListMap = bandsAndSolo(artists.stream());
System.out.println(booleanListMap.get(true));
System.out.println(booleanListMap.get(false));
}
/*
* 分流
*/
public static Map<Boolean, List<Artist>> bandsAndSolo(Stream<Artist> artists) {
return artists.collect(Collectors.partitioningBy(artist -> artist.getMembers().size() > 1));
}
返回结果
[Artist(name=null, members=[1, 2], origin=null)]
[]