How to get ArrayList from Stream in Java 8
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 ArrayList
- Return/Print the ArrayList
Below is the implementation of the above approach:
Program:
// Java program to convert Stream to ArrayList// using Collectors.toList() methodimportjava.util.*;importjava.util.stream.*;publicclassGFG {// Function to get ArrayList from Streampublicstatic<T> ArrayList<T>getArrayListFromStream(Stream<T> stream){// Convert the Stream to ListList<T>list = stream.collect(Collectors.toList());// Create an ArrayList of the ListArrayList<T>arrayList =newArrayList<T>(list);// Return the ArrayListreturnarrayList;}// Driver codepublicstaticvoidmain(String args[]){Stream<Integer>stream = Stream.of(1,2,3,4,5);// Convert Stream to ArrayList in JavaArrayList<Integer>arrayList = getArrayListFromStream(stream);// Print the arraylistSystem.out.println("ArrayList: "+ arrayList);}}chevron_rightfilter_noneOutput: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 ArrayList
Below is the implementation of the above approach:
Program:
// Java program to convert Stream to ArrayList// using Collectors.toList() methodimportjava.util.*;importjava.util.stream.*;publicclassGFG {// Function to get ArrayList from Streampublicstatic<T> ArrayList<T>getArrayListFromStream(Stream<T> stream){// Convert the Stream to ArrayListArrayList<T>arrayList = stream.collect(Collectors.toCollection(ArrayList::new));// Return the ArrayListreturnarrayList;}// Driver codepublicstaticvoidmain(String args[]){Stream<Integer>stream = Stream.of(1,2,3,4,5);// Convert Stream to ArrayList in JavaArrayList<Integer>arrayList = getArrayListFromStream(stream);// Print the arraylistSystem.out.println("ArrayList: "+ arrayList);}}chevron_rightfilter_noneOutput:ArrayList: [1, 2, 3, 4, 5]
Recommended Posts:
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- ArrayList of ArrayList in Java
- Java.util.ArrayList.addall() method in Java
- Arraylist.contains() in Java
- ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.Arraylist.indexOf() in Java
- Reverse an ArrayList in Java
- Vector vs ArrayList in Java
- Synchronization of ArrayList in Java
- Get first and last elements from ArrayList in Java
- ArrayList vs LinkedList in Java
- Arraylist lastIndexOf() in Java with example
- Initialize an ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



