Java streams 30. Collect 6. Collectors.toList(), .toSet(), and .toCollection()

Terminal operation either returns one value (of the same or another type than the type of the input) or does not return anything at all (produces just side effects). It does not allow another operation to be applied after it and closes the stream.

In this post, we will continue covering the last of the terminal operations called collect():

R collect(Collector<T,A,R> collector)

It allows implementing a vast variety of algorithms using the ready-to-use implementations of collectors created by factory methods of the java.util.stream.Collectors class.



Creating List object

The Collectors.toList() is easy to use. If just collects the emitted elements in an object of a class that implements List interface: 

  
  List<String> list = Stream.of("a", "a", "b")
                            .collect(Collectors.toList());
  System.out.print(list);   //prints: [a, a, b] 
    

Alternatively, if you need to produce an object of a particular List implementation, you can use the Collectors.toCollection() collector :

    
  List<String> list2 = Stream.of("a", "a", "b")
       .collect(Collectors.toCollection(LinkedList::new));
  System.out.print(list2);    //prints: [a, a, b]
   

As you can see, all you have to do is to pass the Supplier parameter that returns a new object of the type (the class that implements List interface) you need.



Creating Set object

Similarly, the Collectors.toSet() collector collects the emitted elements in an object of a class that implements Set interface: 

   
  Set<String> set = Stream.of("a", "a", "b")
                          .collect(Collectors.toSet());
  System.out.print(set);   //prints: [a, b]
    

And, if you need to produce an object of a particular Set implementation, you can use the Collectors.toCollection() collector:

   
  Set<String> set2 = Stream.of("a", "a", "b")
       .collect(Collectors.toCollection(TreeSet::new));
  System.out.print(set2);    //prints: [a, b]
   


Creating any Collection

As we have demonstrated already, one can create a collection of any type using Collectors.toCollection() collector. The following are two more examples:

   
  Vector<String> coll1 = Stream.of("a", "a", "b")
       .collect(Collectors.toCollection(Vector::new));
  System.out.print(coll1);     //prints: [a, a, b]

  Stack<String> coll2 = Stream.of("a", "a", "b")
       .collect(Collectors.toCollection(Stack::new));
  System.out.print(coll2);     //prints: [a, a, b]
     

In the next post, we will talk about creating a Map object using Collectors.toMap() collector.

See other posts on Java 8 streams and posts on other topics.
You can also use navigation pages for Java stream related blogs:
— Java 8 streams blog titles
— Create stream
— Stream operations
— Stream operation collect()
The source code of all the code examples is here in GitHub

, ,

Send your comments using the link Contact or in response to my newsletter.
If you do not receive the newsletter, subscribe via link Subscribe under Contact.

Powered by WordPress. Designed by Woo Themes