How to use Collectors.toCollection in Java 8

39 total views, 1 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to use Collectors.toCollection in Java 8.

If you would like to learn about Java 8 Stream operations.Please check out this link.

Collectors.toCollection in Java 8 is used in Stream concepts to collect the stream of elements in a collection and collection should be of type  java.util.Collection.

We can use Collectors.toCollection to collect the stream as ArrayList,LinkedList,HashSet,TreeSet etc.

So the main usage of Collectors.toCollection is,

Accumulate Stream of elements in any collection

Syntax

public static <T,C extends Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory)

Let us now see a few examples.

Collect as ArrayList using Collectors.toCollection

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

        // Collect as ArrayList
        List<String> collectedFruits = fruits.stream().collect(Collectors.toCollection(ArrayList::new));
       
    }
}

Collect as LinkedList using Collectors.toCollection

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

        // Collect as LinkedList
        List<String> collectedFruits = fruits.stream().collect(Collectors.toCollection(LinkedList::new));

    }
}

Collect as HashSet using Collectors.toCollection

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

        // Collect as HashSet
        Set<String> collectedFruits = fruits.stream().collect(Collectors.toCollection(HashSet::new));

    }
}

Collect as TreeSet using Collectors.toCollection

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

        // Collect as TreeSet
        Set<String> collectedFruits = fruits.stream().collect(Collectors.toCollection(TreeSet::new));

    }
}

References : Collectors.toCollection

I hope you like this tutorial. Thanks for reading and Please leave us a comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.