How to get ArrayList from Stream in Java 8 - GeeksforGeeks

11 Dec, 2018
Given a Stream, the task is to convert this Stream into ArrayList in Java 8.
Examples:
Input: Stream: [1, 2, 3, 4, 5]
Output: ArrayList: [1, 2, 3, 4, 5]

Input: Stream: ['G', 'e', 'e', 'k', 's']
Output: ArrayList: ['G', 'e', 'e', 'k', 's']

Using Collectors.toList() method:Get the Stream to be converted.Collect the stream as List using collect() and Collectors.toList() methods.Convert this List into an ArrayListReturn/Print the ArrayListBelow is the implementation of the above approach:Program:// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to List        List<T>            list = stream.collect(Collectors.toList());          // Create an ArrayList of the List        ArrayList<T>            arrayList = new ArrayList<T>(list);          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: " + arrayList);    }}Output:ArrayList: [1, 2, 3, 4, 5]
Using Collectors.toCollection() method:Approach:Get the Stream to be converted.Collect the stream as ArrayList using collect() and Collectors.toCollection() methods.Return/Print the ArrayListBelow is the implementation of the above approach:Program:// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to ArrayList        ArrayList<T>            arrayList = stream                            .collect(Collectors                            .toCollection(ArrayList::new));          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: "                           + arrayList);    }}Output:ArrayList: [1, 2, 3, 4, 5]

Using Collectors.toList() method:Get the Stream to be converted.Collect the stream as List using collect() and Collectors.toList() methods.Convert this List into an ArrayListReturn/Print the ArrayListBelow is the implementation of the above approach:Program:// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to List        List<T>            list = stream.collect(Collectors.toList());          // Create an ArrayList of the List        ArrayList<T>            arrayList = new ArrayList<T>(list);          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: " + arrayList);    }}Output:ArrayList: [1, 2, 3, 4, 5]

Get the Stream to be converted.Collect the stream as List using collect() and Collectors.toList() methods.Convert this List into an ArrayListReturn/Print the ArrayList
Get the Stream to be converted.
Collect the stream as List using collect() and Collectors.toList() methods.
Convert this List into an ArrayList
Return/Print the ArrayList
Below is the implementation of the above approach:
Program:
// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to List        List<T>            list = stream.collect(Collectors.toList());          // Create an ArrayList of the List        ArrayList<T>            arrayList = new ArrayList<T>(list);          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: " + arrayList);    }}
ArrayList: [1, 2, 3, 4, 5]

Using Collectors.toCollection() method:Approach:Get the Stream to be converted.Collect the stream as ArrayList using collect() and Collectors.toCollection() methods.Return/Print the ArrayListBelow is the implementation of the above approach:Program:// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to ArrayList        ArrayList<T>            arrayList = stream                            .collect(Collectors                            .toCollection(ArrayList::new));          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: "                           + arrayList);    }}Output:ArrayList: [1, 2, 3, 4, 5]

Get the Stream to be converted.Collect the stream as ArrayList using collect() and Collectors.toCollection() methods.Return/Print the ArrayList
Get the Stream to be converted.
Collect the stream as ArrayList using collect() and Collectors.toCollection() methods.
Return/Print the ArrayList
Below is the implementation of the above approach:
Program:
// Java program to convert Stream to ArrayList// using Collectors.toList() method  import java.util.*;import java.util.stream.*;  public class GFG {      // Function to get ArrayList from Stream    public static <T> ArrayList<T>    getArrayListFromStream(Stream<T> stream)    {          // Convert the Stream to ArrayList        ArrayList<T>            arrayList = stream                            .collect(Collectors                            .toCollection(ArrayList::new));          // Return the ArrayList        return arrayList;    }      // Driver code    public static void main(String args[])    {          Stream<Integer>            stream = Stream.of(1, 2, 3, 4, 5);          // Convert Stream to ArrayList in Java        ArrayList<Integer>            arrayList = getArrayListFromStream(stream);          // Print the arraylist        System.out.println("ArrayList: "                           + arrayList);    }}
ArrayList: [1, 2, 3, 4, 5]

Java-ArrayList
Java-List-Programs
java-stream
Java-Stream-programs
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java