What is the best, cheapest, performantest way to achive the following:
I do have a Collection
of Objects myObject
, which provide a method returning a Set of Integers. I want to add all the items inside the sets into a new set.
LinkedList<myObject> ll = new LinkedList<>();
//fill the list bla bla
Set<Integer> result = ll.stream()
.map(f -> f.getTheSet())
.flatMap(Set::stream)
.collect(Collectors.toCollection(TreeSet::new));
System.out.println(result.toString());
is there a better way of getting a resulting set containing all integers from all objects?
I would like to avoid "unpacking the set" with the flatMap
command. Instead i think about something like .addAll
or does it in the end not matter, because .addAll
unpacks anyway?