Collection collectors

The Collectors method also provides ways to reduce the stream into mutable data structures such as lists and sets:

//Collection Collectors
List<String>listCollected = streamSupplier.get().collect(Collectors.toList());
System.out.println("The list collected value of Stream are :: " + listCollected);
Set<String>setCollected=streamSupplier.get().collect(Collectors.toSet());
System.out.println("The set collected value of Stream are :: " + setCollected);

By default, the toSet() method produces a HashSet and hence the results are unsorted. If we want the results to be sorted, we can use the toCollection() method to specify an ordered set, in our case let's use Tree Set:

Set<String>orderedSetCollected=streamSupplier.get().collect(Collectors.toCollection(TreeSet::new));
System.out.println("The ordered set collected value of Stream are :: "+orderedSetCollected);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.254.151