1

I have a set of value objects:

Set<EntityKey> clientAssignedPlaceholderEntityKeys

Where the EntityKey class has the following properties:

private Integer objectRef;
private String entityType;

What is the most efficient way to extract the distinct objectRef values into a sorted list using streams?

I have the following but the fact that it calls stream() twice seems like a bad smell:

  // 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());
David Easley
  • 1,347
  • 1
  • 16
  • 24

3 Answers3

4

Maybe:

sortedPlaceholderObjectRefs = clientAssignedPlaceholderEntityKeys.stream()
                      .map(entityKey -> entityKey.getObjectRef())
                      .sorted()
                      .distinct()
                      .collect(Collectors.toList());

EDIT:

calling .distinct() before .sorted() might be more optimal

Mershel
  • 542
  • 1
  • 9
  • 17
2
List<Integer> sortedRefs = clientAssignedPlaceholderEntityKeys
                      .stream()
                      .map(EntityKey::getObjectRef)
                      .distinct()
                      .sorted()
                      .collect(Collectors.toList());
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
2
      clientAssignedPlaceholderEntityKeys.stream()
                                         .map(ek -> ek.getObjectRef())
                                         .sorted()
                                         .distinct()
                                         .collect(Collectors.toList());

Regarding your doubt about the order of sorted() and distinct()

Quoting the very first answer from here: Chaining distinct() operation after sorted(), the implementation will utilize the sorted nature of the data and avoid building an internal HashSet.

redshift
  • 91
  • 5
  • That's good to know @redshift. In case future readers might get confused, could you please edit your answer to show distinct() operation after sorted()? I will then accept this answer. – David Easley Oct 26 '18 at 17:08