partitioningBy分区是一种特殊的分组,可以将List划分为两个子List,使用起来比较简单,如下:
@Test
public void partitionByTest(){
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
Map<Boolean, List<Integer>> groups =
intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
List<Integer> lastPartition = subSets.get(1);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(2));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
通过上述的例子看一看出,通过partitionBy将intList划分为大于6和小于等于6的两个子List。本来打算跟groupingBy一起写的,但是觉得这个partitionBy也跟groupingBy一样有很多示例,结果看了之后才发现是个很简单的东西 ==