我有一组值对象: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());
3 回答

www说
TA贡献1775条经验 获得超8个赞
也许:
sortedPlaceholderObjectRefs = clientAssignedPlaceholderEntityKeys.stream() .map(entityKey -> entityKey.getObjectRef()) .sorted() .distinct() .collect(Collectors.toList());
编辑:
.distinct()
之前调用.sorted()
可能更优化

MMTTMM
TA贡献1869条经验 获得超4个赞
List<Integer> sortedRefs = clientAssignedPlaceholderEntityKeys .stream() .map(EntityKey::getObjectRef) .distinct() .sorted() .collect(Collectors.toList());
添加回答
举报
0/150
提交
取消