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:
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
class GFG {
private static <T> Stream<T> convertListToStream(List<T> list)
{
return list.stream();
}
public static void main(String args[])
{
List<String> list = Arrays.asList( "GeeksForGeeks" ,
"A computer portal" ,
"for Geeks" );
System.out.println( "List: " + list);
Stream<String> stream = convertListToStream(list);
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:
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
class GFG {
public static void main(String args[])
{
List<String> list = Arrays.asList( "GeeksForGeeks" ,
"A computer portal" ,
"for" ,
"Geeks" );
System.out.println( "List: " + list);
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s)
{
return s.startsWith( "G" );
}
};
System.out.println( "Stream from List with items" +
" starting with G: " );
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!