假设我们有一些实体,每个实体都有一个可搜索字段和类型的列表。是否有更好的(阅读更有效的方法)为每种不同类型的实体映射列表中的这些字段。
目前我正在做的是:
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/