Java 8使用com.google.common.collect.Iterables.partition循环的方式?

Joe*_*oeG 1 java guava java-8

看起来像这应该是Stream.of一些简单的使用方式,但....

这是我想要改进的代码(myEntryIdsLong几千个项目的长度列表):

List<MyEntityType> results = new ArrayList<>();

// batch up into groups of 1000 
for (final List<Long> partitionedEntryIds : 
       com.google.common.collect.Iterables.partition(myEntryIds, 1000)) {
        results.addAll(BeanConverter.convertList(
             myJpaRepository.findAll(partitionedEntryIds)));
}

return results;
Run Code Online (Sandbox Code Playgroud)

Xae*_*ess 5

Iterables#partition在JDK流中没有等价物,但您可以使用Guava的Streams#stream帮助toImmutableList()器和收集器(以及我个人喜欢的一些方法引用)来实现您的其他目标:

final List<MyEntityType> myEntityTypes = Streams.stream(
    Iterables.partition(myEntryIds, 1000))
    .map(myJpaRepository::findAll)
    .map(BeanConverter::convertList)
    .flatMap(List::stream)
    .collect(toImmutableList());
Run Code Online (Sandbox Code Playgroud)