In this article, we will discuss how to convert Stream into a LinkedHashSet in Java 1.8 version using Stream API. LinkedHashSet doesn’t allow duplicates but maintains insertion-order.
Stream to LinkedHashSet :
- Using Collectors.toSet()
- Using Collectors.toCollection()
1. Using Collectors.toSet()
- First, convert Stream to Set using collect() method of Stream API by passing Collectors.toSet() as input argument
- Above conversion yields Set and not LinkedHashSet
- For Set to LinkedHashSet conversion, create LinkedHashSet object and pass above set as constructor-argument
- Finally, print converted LinkedHashSet elements to console
StreamToLinkedHashSetUsingCollectorsToSet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package net.bench.resources.stream.to.list; import java.util.LinkedHashSet; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamToLinkedHashSetUsingCollectorsToSet { public static void main(String[] args) { // 1. Stream of String tokens Stream<String> nameStream = Stream.of( "Rajiv" , "Anbu" , "Santosh" , "Abdul" , "Lingaraj" ); // 2. convert Stream<String> to Set<String> Set<String> names = nameStream.collect(Collectors.toSet()); // 3. Set<String> to LinkedHashSet<String> LinkedHashSet<String> lhSetNames = new LinkedHashSet<String>(names); // 4. print to console System.out.println( "Stream to LinkedHashSet : \n\n" + lhSetNames); } } |
Output:
1 2 3 | Stream to LinkedHashSet : [Lingaraj, Abdul, Rajiv, Anbu, Santosh] |
2. Using Collectors.toCollection()
- Convert Stream to LinkedHashSet using collect() method of Stream API by passing Collectors.toCollection(LinkedHashSet::new) as input argument directly
- Print converted LinkedHashSet elements to console
StreamToLinkedHashSetUsingCollectorsToCollection.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package net.bench.resources.stream.to.list; import java.util.LinkedHashSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamToLinkedHashSetUsingCollectorsToCollection { public static void main(String[] args) { // 1. Stream of String tokens Stream<String> nameStream = Stream.of( "Rajiv" , "Anbu" , "Santosh" , "Abdul" , "Lingaraj" ); // 2. convert Stream<String> to LinkedHashSet<String> LinkedHashSet<String> lhSetNames = nameStream .collect(Collectors.toCollection(LinkedHashSet:: new )); // 3. print to console System.out.println( "Stream to LinkedHashSet : \n\n" + lhSetNames); } } |
Output:
1 2 3 | Stream to LinkedHashSet : [Rajiv, Anbu, Santosh, Abdul, Lingaraj] |
Related Articles:
- Java 8 – Conversion of Arrays to Stream
- Java 8 – Conversion of Stream to Arrays
- Java 8 – Convert List to Stream
- Java 8 – Convert Stream to List
- Java 8 – Convert Stream to ArrayList
- Java 8 – Convert Stream to LinkedList
- Java 8 – Convert Stream to HashSet
- Java 8 – Convert Stream to LinkedHashSet
- Java 8 – Convert Stream to TreeSet
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toSet–
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toCollection-java.util.function.Supplier-
Happy Coding !!
Happy Learning !!