How to get ArrayList from Stream in Java 8 - Collectors Example, Tutorial

You can use Collectors.toList(), toSet() and toMap() to get all elements of Stream into any Collection e.g. List, Set or Map, but if you want to get a particular collection e.g. ArrayList, then you need to use Collectors.toCollection(ArrayList::new) method. This method first creates an ArrayList using method reference and then adds all elements of Stream into the ArrayList. It's very useful if you have a long list of String and you want to create a smaller list containing only String starting with the letter "b" e.g. "Bluehost". All you need to do is first get the stream from List by calling stream() method, then call the filter() method to create a new Stream of filtered values and finally call the Collectors.toCollection(ArrayList::new) to collect those elements into an ArrayList. Let's see a couple of examples to understand the concept better.

The class is provided to perform useful reduction operations e.g. accumulating stream elements into collections like the list, set or map. It provides convenient utility methods like toList() to get a list of elements from Stream, toSet() to get elements inside a Set from Stream and toMap() to create a Map from the object stored in Stream. If you don't want to collect but just want to print, you can use the forEach() method to print all elements of Stream in Java 8.

But when you use toList() method there are no guarantees on the type, mutability, serializability, or thread-safety of the List returned, that's why if you need an ArrayList, you should use toCollection() method. You can also see Java SE 8 for Really Impatient to learn more about effective use of Collectors in Java 8.

How to get ArrayList from Stream in Java 8

Java Program to get ArrayList from Stream in Java 8

Here is our sample Java program to demonstrate how to get the ArrayList from the Stream in Java. It shows how you can use the Collectors to collect the result in various collections. Btw, you can use static import feature of Java to make the code more readable, just statically import Collectors methods and instead of writing Collectors. collect() just write collect() or Collectors.toList() just write the toList(). This will make your code concise and more readable.

How to convert Stream to ArrayList in Java 8 - Collector example

Remember when you call toList(), toSet() or toMap() method it's not guaranteed which implementation will return. If you want a specific collection type then just ask for it e.g. we have asked for ArrayLsit here. Similarly, if you are converting Stream to Map, you can ask for LinkedHashMap if you want to preserve order.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

 * Java Program to show how to get the ArrayList
 * from Stream in Java 8. 
public class Java8Demo {

    public static void main(String args[]) {

        List<Integer> numbers = Arrays.asList(100, 12001, 2001, 2003, 120, 20, 3, 4);
        System.out.println("original list: " + numbers);

        // converting Stream to List in Java
        List<Integer> listOf100s =
                .filter(num -> num > 100)
        System.out.println("list of numbers greater than 100: " + listOf100s);

        // Converting Stream to ArrayList in Java
        List<Integer> listOf200s =
                .filter(num -> num > 200)
        System.out.println("list of numbers greater than 200: " + listOf200s);


original list: [100, 12001, 2001, 2003, 120, 20, 3, 4]
list of numbers greater than 100: [12001, 2001, 2003, 120]
list of numbers greater than 200: [12001, 2001, 2003]

That's all about How to get an ArrayList from Stream in Java 8. You need to use the Collectors class and it's toCollection() method. This method accepts a Supplier, which returns a new, empty Collection of an appropriate type. Just remember that ArrayList will preserve the order only if Stream is not parallel. For parallel Stream, ArrayList might have elements in different orders as they were added.

1 comment: