How to Get ArrayList from Stream in Java 8?
Last Updated :
17 Jul, 2024
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']
1. Using Collectors.toList() method:
Approach:
- 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
// Java program to convert Stream to ArrayList
// using Collectors.toList() method
import java.util.*;
import java.util.stream.*;
public class GFG {
// 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 = new ArrayList<Integer>(stream.collect(Collectors.toList()));
// Print the arraylist
System.out.println("ArrayList: " + arrayList);
}
}
OutputArrayList: [1, 2, 3, 4, 5]
Explanation of the Program:
- This Java program demonstrates converting a Stream to an ArrayList using the
Collectors.toList
method. - In the
main
method, a Stream of integers is created and then collected into a List, which is used to initialize an ArrayList. - The resulting ArrayList is then printed to the console.
2. 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
// 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);
}
}
OutputArrayList: [1, 2, 3, 4, 5]
Explanation of the Program:
- This Java program converts a Stream to an ArrayList using the
Collectors.toCollection
method. - The
getArrayListFromStream
function takes a Stream as input, collects its elements into an ArrayList, and returns it. - In the
main
method, a Stream of integers is converted into an ArrayList and printed.
Similar Reads
Java List Interface The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features:Maintained the order of elements in which they are added.Allows
15+ min read
AbstractList in Java with Examples The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Rand
7 min read
ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac
9 min read
LinkedList in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an
12 min read
Immutable List in Java ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown. An I
5 min read
CopyOnWriteArrayList in Java CopyOnWriteArrayList class is introduced in JDK 1.5, which implements the List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy. It is found in java.util.concurrent package. It is a data structure created to b
6 min read
Custom ArrayList in Java Before proceeding further let us quickly revise the concept of the arrays and ArrayList quickly. So in java, we have seen arrays are linear data structures providing functionality to add elements in a continuous manner in memory address space whereas ArrayList is a class belonging to the Collection
8 min read
Difference Between Synchronized ArrayList and CopyOnWriteArrayList in Java Collection As we know that the ArrayList is not synchronized, if multiple threads try to modify an ArrayList at the same time, then the final outcome will be non-deterministic. Hence synchronizing the ArrayList is a must to achieve thread safety in a multi-threaded environment. In order to make List objects we
6 min read
How to remove a SubList from a List in Java Given a list in Java, the task is to remove all the elements in the sublist whose index is between fromIndex, inclusive, and toIndex, exclusive. The range of the index is defined by the user. Example: Input list = [1, 2, 3, 4, 5, 6, 7, 8], fromIndex = 2, endIndex = 4 Output [1, 2, 5, 6, 7, 8] Input
2 min read
Randomly Select Items from a List in Java In this article, we will explore how to efficiently select an element from a list in Java. The basic approach involves generating a random index between 0 and the size of the list, and then using that index to retrieve the item. Different Ways to Select Items from a ListBelow are the different appro
3 min read