How to convert Stream to Set using java


In Java 8, Stream is one of the most important class as it allows a lot of useful functional operations e.g. filter, map, flatmap, etc on a collection of object. Hence, going forward converting a Collection to Stream, performing operations and then converting the result back to different Collection classes like List, Set, and Map will be a common task. 
You can convert a Stream to Set by using the Collectors.toSet() method. Since Set doesn't allow duplicates and doesn't provide any ordering guarantee, any duplicate elements from Stream are lost and ordering is also gone.
Set<Integer> aSet = input.stream().collect(Collectors.toSet());

There are no implementation guarantees provided for the Set returned by collect method here. You can't assume it a HashSet, but it will something which implements Set interface. Also, the size of Set will be less than or equal to a number of elements in the final Stream because duplicate elements are lost when you covert Stream to Set.
 

Stream to HashSet

If you need a HashSet rather than a Set then you need to use the toCollection() method rather than the toSet() method. As we have seen in the case of ArrayList, the toCollection() method allows you to specify which type of Collection class you want by providing a supplier. Here is an example for converting Stream to HashSet in Java.
HashSet<Integer> anHashSet
           = input.stream()
                  .collect(Collectors.toCollection(HashSet::new));

As with any other Set, when you convert Stream to HashSet, all duplicate elements will be lost and order of elements will be gone.




Stream to LinkedHashSet

Similar to HashSet, you can also use toCollection() method of Collectors class to convert a Stream to LinkedHashSet in Java. The main difference between HashSet and LinkedHashSet is that order is preserved. Elements in the LinkedHashSet exists in the order they are inserted. Btw, If you are interested in learning more differences you can see my earlier article HashSet vs LinkedHashSet in Java.
LinkedHashSet<Integer> aLinkedHashSet
              = input.stream()
                     .collect(Collectors.toCollection(LinkedHashSet::new));

Again, like any other Set, duplicate elements from Stream will be lost, hence the size of the LinkedHashSet will be less than or equal to the number of elements in the final Stream.



Stream to TreeSet

Similar to earlier examples, you can also use the toCollection() method of Collectors class to convert a Stream to TreeSet in Java. Though, you should remember that TreeSet is a sorted Set and keeps the elements in their natural order or a custom order specified by Comparator.
TreeSet<Integer> aTreeSet 
        = input.stream()
                .collect(Collectors.toCollection(TreeSet::new));

In this example, elements in the TreeSet will be in their sorted order which may be different from their order in Stream. Again, any duplicate element will be lost because TreeSet doesn't allow duplicate and hence the size of TreeSet will be less than or equal to the size of the final Stream.

If you are interested to learn more about different collection classes and how to work with them using Stream, see From Collections to Streams in Java 8 Using Lambda Expressions course on Pluralsight.



Related blog:

Spring boot interview questions


Comments

Popular posts from this blog

Java Concurrency Method basic example

How to Learn Java Effectively