In this article, we will discuss how to convert List into Stream in Java 1.8 version using stream() method
1. Using stream() method
- List to Stream is a straight-forward conversion using stream() method
- After converting to Stream, further processing is also possible through series of intermediate operations but final result won’t be yielded until terminal operation is executed
- Read Intermedicate operation v/s terminal operation
ListToStreamConversion.java
package net.bench.resources.stream.to.list;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ListToStreamConversion {
public static void main(String[] args) {
// 1. define list
List<String> names = Arrays.asList(
"Rajiv",
"Anbu",
"Santosh",
"Abdul",
"Lingaraj"
);
// 2. convert List to Stream
Stream<String> nameStream = names.stream();
// 3. print to Stream elements using forEach looping
System.out.println("Stream elements : \n");
nameStream.forEach(name -> System.out.println(name));
}
}
Output:
Stream elements :
Rajiv
Anbu
Santosh
Abdul
Lingaraj
2. List to Stream :
- Using stream() method
- Convert to Stream and then filter using filter() method of Stream API
- Convert to Stream and then tranform to another form using map() method of Stream API
2.1 Filter a Stream using Predicate
- We can apply Predicate to Stream elements using filter() method of Stream API after converting List to Stream to remove unwanted items/elements from Stream (or in other words to get desired result)
- Predicate can be defined separately and its instance can be passed to filter() method or pass lambda-expression as mentioned below
- Lambda-expression :- filter(name -> name.startsWith(“A”))
ConvertToStreamAndFilter.java
package net.bench.resources.stream.to.list;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ConvertToStreamAndFilter {
public static void main(String[] args) {
// 1. define list
List<String> names = Arrays.asList(
"Rajiv",
"Anbu",
"Santosh",
"Abdul",
"Lingaraj"
);
// 2. convert List to Stream
Stream<String> nameStream = names.stream();
// 3. print to console
System.out.println("Stream filtering : \n");
// 4. filter and print
nameStream
.filter(name -> name.startsWith("A"))
.forEach(System.out::println);
}
}
Output:
Stream filtering :
Anbu
Abdul
2.2 Map a Stream using Function
- Function can be applied to Stream elements using map() method of Stream API after converting List to Stream
- map() method transform Stream elements from one form to another form for example, converting all cases of Stream elements into upper case
- Function can be defined separately and its instance can be passed to map() method or pass lambda-expression as mentioned below
- Lambda-expression :- map(name -> name.toUpperCase())
ConvertToStreamAndMapToUpperCase.java
package net.bench.resources.stream.to.list;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ConvertToStreamAndMapToUpperCase {
public static void main(String[] args) {
// 1. define list
List<String> names = Arrays.asList(
"Rajiv",
"Anbu",
"Santosh",
"Abdul",
"Lingaraj"
);
// 2. convert List to Stream
Stream<String> nameStream = names.stream();
// 3. print to console
System.out.println("Stream elements in Upper case : \n");
// 4. filter and print
nameStream
.map(name -> name.toUpperCase())
.forEach(System.out::println);
}
}
Output:
Stream elements in Upper case :
RAJIV
ANBU
SANTOSH
ABDUL
LINGARAJ
Related Articles:
- Java 8 – Conversion of Arrays to Stream
- Java 8 – Conversion of Stream to Arrays
- Java 8 – Convert List to Stream
- Java 8 – Convert Stream to List
- Java 8 – Convert Stream to ArrayList
- Java 8 – Convert Stream to LinkedList
- Java 8 – Convert Stream to HashSet
- Java 8 – Convert Stream to LinkedHashSet
- Java 8 – Convert Stream to TreeSet
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#stream–
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#filter-java.util.function.Predicate-
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-
Happy Coding !!
Happy Learning !!