Java 8 – Convert List to Stream

In this article, we will discuss how to convert List into Stream in Java 1.8 version using stream() method

List to Stream :

  • Using stream() method
    1. Convert to Stream and then filter using filter() method of Stream API
    2. Convert to Stream and then tranform to another form using map() method of Stream API

1. Using stream() method

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

1.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

1.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

References:

Happy Coding !!
Happy Learning !!

Java 8 - Convert Stream to ArrayList
Java 8 - Convert Stream to List