我有一组值对象:
Set<EntityKey> clientAssignedPlaceholderEntityKeys
其中 EntityKey 类具有以下属性:
private Integer objectRef;
private String entityType;
使用流将不同的 objectRef 值提取到排序列表中的最有效方法是什么?
我有以下内容,但它两次调用 stream() 的事实似乎很难闻:
// Extract into a sorted list all the distinct placeholder objectRefs (regardless of type).
List<Integer> sortedPlaceholderObjectRefs = clientAssignedPlaceholderEntityKeys.stream()
.map(entityKey -> entityKey.getObjectRef())
.collect(Collectors.toSet())
.stream() // having to call stream() a 2nd time here feels sub-optimal
.sorted()
.collect(Collectors.toList());
最佳答案
也许:
sortedPlaceholderObjectRefs = clientAssignedPlaceholderEntityKeys.stream()
.map(entityKey -> entityKey.getObjectRef())
.sorted()
.distinct()
.collect(Collectors.toList());
编辑:
在 .sorted()
之前调用 .distinct()
可能更优化
关于java - 如何使用 Java 流从一组对象中提取不同整数的排序列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004860/