The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Below are various methods to convert List to Stream in Java:
- Using List.stream() method: Java List interface provides stream() method which returns a sequential Stream with this collection as its source.
Syntax:
List.stream()
Algorithm:
- Get the Stream
- Convert Stream into List using List.stream() method.
- Return the List
Program:
// Java Program to convert
// List to Stream in Java 8
import
java.util.*;
import
java.util.stream.*;
import
java.util.function.Function;
class
GFG {
// Generic function to convert a list to stream
private
static
<T> Stream<T> convertListToStream(List<T> list)
{
return
list.stream();
}
public
static
void
main(String args[])
{
// Create a stream of integers
List<String> list = Arrays.asList(
"GeeksForGeeks"
,
"A computer portal"
,
"for Geeks"
);
// Print the List
System.out.println(
"List: "
+ list);
// Convert List to stream
Stream<String> stream = convertListToStream(list);
// Print the Stream
System.out.println(
"Stream from List: "
+ Arrays.toString(stream.toArray()));
}
}
Output:List: [GeeksForGeeks, A computer portal, for Geeks] Stream from List: [GeeksForGeeks, A computer portal, for Geeks]
- Filter Stream using a Predicate: The Functional Interface Predicate is defined in the java.util.Function package and can therefore be used as the assignment target for a lambda expression or method reference. It improves manageability of code, helps in unit-testing them separately
Algorithm:
- Get the Stream
- Define the Predicate condition by either using pre-defined static methods or by creating a new method by overriding the Predicate interface. In this program, the interface is overridden to match the strings that start with “G”.
- Convert Stream into List using List.stream() method.
- Filter the obtained stream using the defined predicate condition
- The required Stream has been obtained.
Hence Print the filtered elements of the Stream using forEach() method.
It can also be returned as required.
Program:
// Java Program to convert
// List to Stream in Java 8
import
java.util.*;
import
java.util.stream.*;
import
java.util.function.*;
class
GFG {
public
static
void
main(String args[])
{
// Create a stream of integers
List<String> list = Arrays.asList(
"GeeksForGeeks"
,
"A computer portal"
,
"for"
,
"Geeks"
);
// Print the List
System.out.println(
"List: "
+ list);
// Create the predicate for item starting with G
Predicate<String> predicate =
new
Predicate<String>() {
@Override
public
boolean
test(String s)
{
// filter items that start with "G"
return
s.startsWith(
"G"
);
}
};
System.out.println(
"Stream from List with items"
+
" starting with G: "
);
// Convert List to stream
list.stream()
.filter(predicate)
.forEach(System.out::println);
}
}
Output:List: [GeeksForGeeks, A computer portal, for, Geeks] Stream from List with items starting with G: GeeksForGeeks Geeks