Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Stream to List

In this post, we will see how to convert Stream to List in java.

There are multiple ways to convert Stream to List in java.

Using Collectors.toList()

You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides a collector which accumulates elements of Stream into the list.

Here is an quick example:

package org.arpit.java2blog;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConvertStreamToListMain {
    public static void main(String args[])
    {
        Stream countriesStream = Stream.of("India", "China", "France", "Germany");
        List listOfCountiesName = countriesStream.collect(Collectors.toList());
        System.out.println(listOfCountiesName);
    }
}

Output:

[India, China, France, Germany]

Using Collectors.toCollection()

You can pass Collectors.toCollection() to Stream.collect() method to convert Stream to List in java. This is similar to previous approach, but it gives you more control over type of collection you want.

Here is an example to create LinkedList from Stream.

package org.arpit.java2blog;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConvertStreamToListMain {
    public static void main(String args[])
    {
        Stream countriesStream = Stream.of("India", "China", "France", "Germany");
        List listOfCountiesName = countriesStream.collect(Collectors.toCollection(LinkedList::new));
        System.out.println(listOfCountiesName);
    }
}
[India, China, France, Germany]

Using foreach

You can iterate over Stream using foreach() method and add elements to the List.

Here is an example to create LinkedList from Stream.

package org.arpit.java2blog;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class ConvertStreamToListMain {
    public static void main(String args[])
    {
        Stream countriesStream = Stream.of("India", "China", "France", "Germany");
        List listOfCountiesName=new ArrayList();
        countriesStream.forEach(listOfCountiesName::add);
        System.out.println(listOfCountiesName);
    }
}

This approach is not recommended in case you are using Parallel Stream as elements of the Streams may not be processed in original order. You can use forEachOrdered() method to preserve the order in the List.
Output:

[India, China, France, Germany]

Filter Stream and convert to List

If you want to filter elements from Stream, you can use Stream’s filter method.

package org.arpit.java2blog;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConvertStreamToListMain {
    public static void main(String args[])
    {
        Stream countriesStream = Stream.of("India", "China", "France", "Germany","Indonesia");
        List listOfCountiesName = countriesStream.filter(country ->country.startsWith("I"))
                                                         .collect(Collectors.toList());
        System.out.println(listOfCountiesName);
    }
}

Output:

[India, Indonesia]

Filter method accepts predicate functional interface to filter a Stream.

Convert infinite Stream to List

You need to limit infinite Stream and then convert it to list.

Here is an exmple:

package org.arpit.java2blog;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ConvertStreamToListMain {
    public static void main(String args[])
    {
        IntStream infiniteIntStream = IntStream.iterate(100, i -> i+1);

        List intList = infiniteIntStream.limit(5)
                                                        .boxed()
                                                        .collect(Collectors.toList());
        System.out.println(intList);
    }
}

Output:

[100, 101, 102, 103, 104]

Here we have used iterate() method to generate an infinite Stream and limited it using limit() method and then converted it to list.

That’s all about Java Stream to List.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Java Stream to List

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×