gpt4 book ai didi

java - 在流式传输另一个列表时将数据添加到列表中

转载 作者:行者123 更新时间:2023-11-30 06:47:07 25 4
gpt4 key购买 nike

假设我们有一些实体,每个实体都有一个可搜索字段列表和一个类型。是否有更好的(阅读更有效的方法)将这些字段映射到每个不同类型的实体的列表中。

目前我正在做的是:

final Collection<IndexedField> indexedFields = new ArrayList<>();
for (String type : types) {
final Class<? extends IndexedEntity> targetClass = indexedEntities.getClassByType(type);
indexedFields.addAll(indexedEntities.getSearchFieldsFor(targetClass));
}

这行得通,但是有没有更好的方法来实现这一点?也许有流 api 的东西。

最佳答案

如果我理解正确的话:

 types.stream()
.map(indexedEntities::getClassByType)
.flatmap(x -> indexedEntities.getSearchFieldsFor(x).stream())
.collect(Collectors.toList());

关于java - 在流式传输另一个列表时将数据添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46489251/

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