@Eugene的回答很甜蜜,因为番石榴很甜。但是,如果您碰巧在类路径中没有番石榴,这是另一种方式:
List<Set<Integer>> list = block.stream() .flatMap(Set::stream) .sorted() .collect(partitioning(3));
首先,我将所有集合映射到一个流中,然后对所有元素进行排序,最后,将整个排序后的流收集到集合列表中。为此,我正在调用使用自定义收集器的辅助方法:
private static <T> Collector<T, ?, List<Set<T>>> partitioning(int size) { class Acc { int count = 0; List<Set<T>> list = new ArrayList<>(); void add(T elem) { int index = count++ / size; if (index == list.size()) list.add(new linkedHashSet<>()); list.get(index).add(elem); } Acc merge(Acc another) { another.list.stream().flatMap(Set::stream).forEach(this::add); return this; } } return Collector.of(Acc::new, Acc::add, Acc::merge, acc -> acc.list);}该方法接收每个分区的大小,并使用
Acc本地类作为收集器要使用的可变结构。在
Acc类内部,我正在使用一个
List包含
linkedHashSet实例的实例,该实例将保存流的元素。
将
Acc类保存所有已已收集到的元素的个数。在该
add方法中,我计算列表的索引并递增此计数,如果列表的该位置没有设置,则将新的空值附加
linkedHashSet到该位置。然后,将元素添加到集合中。
在调用
sorted()流对元素进行收集之前对其进行排序时,我需要使用保留插入顺序的数据结构。这就是为什么我要使用
ArrayList外部列表和
linkedHashSet内部集合。
该
merge方法将由并行流使用,以合并两个先前累积的
Acc实例。我只是通过委托给方法,将接收到的
Acc实例的所有元素添加到该
Acc实例中
add。
最后,我正在使用
Collector.of基于
Acc类方法的收集器。最后一个参数是装订器功能,它仅返回
Acc实例的列表。



