A List in Java is an ordered collection that allows duplicate elements and provides positional access to its elements. The Stream API provides a convenient way to process collection data using operations such as filtering, mapping, and sorting.
- A Stream does not store elements; it processes elements from a source such as a List.
- Stream operations can be combined into a pipeline to perform multiple operations on collection data.
- A Stream can be consumed using terminal operations such as forEach(), collect(), count(), and toArray().
Methods to Convert List to Stream in Java
1. Using List.stream() Method
The stream() method is provided by the Collection interface and can be used directly with a List. It returns a sequential Stream containing the elements of the List.
Syntax:
list.stream();
Steps
- Create a List containing the required elements.
- Call the stream() method on the List.
- Store the returned Stream.
- Process or display the Stream elements using a terminal operation.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// Create a List
List<String> list = Arrays.asList(
"Java", "Python", "C++", "JavaScript");
// Display the original List
System.out.println("List: " + list);
// Convert List to Stream
Stream<String> stream = list.stream();
// Display Stream elements
System.out.println("Stream elements:");
stream.forEach(System.out::println);
}
}
Output
List: [Java, Python, C++, JavaScript] Stream elements: Java Python C++ JavaScript
Explanation: The list.stream() statement creates a sequential Stream from the List. The forEach() method is then used to process each element of the Stream.
2. Filter Stream Using filter()
After converting a List into a Stream, we can use the filter() method to select only the elements that satisfy a particular condition. The filter() method takes a Predicate as an argument. A Predicate is a functional interface that returns either true or false.
Steps
- Create a List.
- Convert the List into a Stream using stream().
- Define a condition inside filter().
- Process only the elements for which the condition returns true.
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a List
List<String> list = Arrays.asList(
"Java",
"Python",
"JavaScript",
"C++",
"JDBC"
);
// Display the original List
System.out.println("List: " + list);
// Convert List to Stream and filter elements
System.out.println("Elements starting with J:");
list.stream()
.filter(name -> name.startsWith("J"))
.forEach(System.out::println);
}
}
Output
List: [Java, Python, JavaScript, C++, JDBC] Elements starting with J: Java JavaScript JDBC
Explanation: Here, list.stream() converts the List into a Stream. The filter() method checks each element using the condition name.startsWith("J"). Only elements beginning with "J" are passed to forEach() and displayed.
3. Using parallelStream()
Java also provides the parallelStream() method to create a parallel Stream from a List. It can process elements concurrently using multiple threads. It can be useful when working with large collections and computationally expensive operations, although it should not be used automatically for every task.
Syntax
list.parallelStream();
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a List
List<Integer> numbers = Arrays.asList(
10, 20, 30, 40, 50
);
System.out.println("List: " + numbers);
System.out.println("Elements processed using parallel stream:");
numbers.parallelStream()
.forEach(System.out::println);
}
}
Output
List: [10, 20, 30, 40, 50] Elements processed using parallel stream: 30 20 10 40 50
Explanation: The parallelStream() method creates a parallel Stream from the List. Unlike a sequential Stream, the elements can be processed by multiple threads, so the output order may differ from the original List.
Note: If maintaining the original order is important, avoid relying on parallelStream().forEach(). You can use forEachOrdered() when ordered processing is required.