在java 8中将列表拆分为具有固定数量元素的多个列表 [英] Split list into multiple lists with fixed number of elements in java 8
问题描述
我想要一些类似于 scala 分组函数的东西.基本上,一次选择 2 个元素并处理它们.这是相同的参考:
I want to something which is similar to the scala grouped function. Basically, pick 2 elements at a time and process them. Here is a reference for the same :
Lambdas 确实提供了 groupingBy 和 partitioningBy 之类的东西,但它们似乎都没有像 Scala 中的 grouped 函数那样做.任何指针将不胜感激.
Lambdas do provide things like groupingBy and partitioningBy but none of them seem to do the same as the grouped function in Scala. Any pointers would be appreciated.
推荐答案
这听起来像是一个问题,像 提供的 ops 一样,像低级
API 本身.一个(相对)简单的解决方案可能如下所示:Stream
操作一样更好地处理Stream
It sounds like a problem that is better handled like a low-level Stream
operation just like the ops provided by the Stream
API itself. A (relative) simple solution may look like:
public static <T> Stream<List<T>> chunked(Stream<T> s, int chunkSize) {
if(chunkSize<1) throw new IllegalArgumentException("chunkSize=="+chunkSize);
if(chunkSize==1) return s.map(Collections::singletonList);
Spliterator<T> src=s.spliterator();
long size=src.estimateSize();
if(size!=Long.MAX_VALUE) size=(size+chunkSize-1)/chunkSize;
int ch=src.characteristics();
ch&=Spliterator.SIZED|Spliterator.ORDERED|Spliterator.DISTINCT|Spliterator.IMMUTABLE;
ch|=Spliterator.NONNULL;
return StreamSupport.stream(new Spliterators.AbstractSpliterator<List<T>>(size, ch)
{
private List<T> current;
@Override
public boolean tryAdvance(Consumer<? super List<T>> action) {
if(current==null) current=new ArrayList<>(chunkSize);
while(current.size()<chunkSize && src.tryAdvance(current::add));
if(!current.isEmpty()) {
action.accept(current);
current=null;
return true;
}
return false;
}
}, s.isParallel());
}
简单测试:
chunked(Stream.of(1, 2, 3, 4, 5, 6, 7), 3)
.parallel().forEachOrdered(System.out::println);
优点是您不需要所有项目的完整集合用于后续流处理,例如
The advantage is that you do not need a full collection of all items for subsequent stream processing, e.g.
chunked(
IntStream.range(0, 1000).mapToObj(i -> {
System.out.println("processing item "+i);
return i;
}), 2).anyMatch(list->list.toString().equals("[6, 7]")));
将打印:
processing item 0
processing item 1
processing item 2
processing item 3
processing item 4
processing item 5
processing item 6
processing item 7
true
而不是处理一千个IntStream.range(0, 1000)
.这也允许使用无限源 Stream
s:
rather than processing a thousand items of IntStream.range(0, 1000)
. This also enables using infinite source Stream
s:
chunked(Stream.iterate(0, i->i+1), 2).anyMatch(list->list.toString().equals("[6, 7]")));
<小时>
如果您对完全物化的集合感兴趣而不是应用后续的 Stream
操作,您可以简单地使用以下操作:
If you are interested in a fully materialized collection rather than applying subsequent Stream
operations, you may simply use the following operation:
List<Integer> list=Arrays.asList(1, 2, 3, 4, 5, 6, 7);
int listSize=list.size(), chunkSize=2;
List<List<Integer>> list2=
IntStream.range(0, (listSize-1)/chunkSize+1)
.mapToObj(i->list.subList(i*=chunkSize,
listSize-chunkSize>=i? i+chunkSize: listSize))
.collect(Collectors.toList());
这篇关于在java 8中将列表拆分为具有固定数量元素的多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!