gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:28:16 29 4
gpt4 key购买 nike

以某种方式使用 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;

最佳答案

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());

关于Java 8 循环使用 com.google.common.collect.Iterables.partition 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187774/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com