Java 8 种使用 com.google.common.collect.Iterables.partition 循环的方法?
Java 8 way to loop using com.google.common.collect.Iterables.partition?
看起来这应该很简单,以某种方式使用 Stream.of
,但是......:)
这是我想要改进的代码(myEntryIds
是 Long
几千项长度的列表):
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;
JDK 流中没有 Iterables#partition
等价物,但您可以使用来自 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());
看起来这应该很简单,以某种方式使用 Stream.of
,但是......:)
这是我想要改进的代码(myEntryIds
是 Long
几千项长度的列表):
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;
JDK 流中没有 Iterables#partition
等价物,但您可以使用来自 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());