Java 8 – Convert Stream to List

In this article, we will discuss different ways to convert Stream to List.

Stream to List :

  1. Using Collectors.toList() method
  2. Using Collectors.toCollection() method
  3. Iterate Stream using forEach loop and convert to List
  4. Convert Infinite Stream to List
  5. Convert Stream to an Array and then to List
  6. Process Stream and convert to List
    • Filter Stream before converting to List
    • Map Stream elements and convert to List

1. Using Collectors.toList() method

  • collect() is the terminal method of Stream API which accepts java.util.stream.Collectors class
  • Pass Collectors.toList() as input argument to collect() method which converts Stream into List
  • This strictly converts Stream into List

UsingCollectorsToList.java

package net.bench.resources.stream.to.list;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UsingCollectorsToList {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to List<String>
		List<String> names = nameStream.collect(Collectors.toList());


		// 3. print to console
		System.out.println(names);
	}
}

Output:

[Rajiv, Anbu, Santosh, Abdul, Lingaraj]

2. Using Collectors.toCollection() method

  • collect() is the terminal method of Stream API which accepts java.util.stream.Collectors class
  • Pass Collectors.toCollection() as input argument to collect() method which converts Stream into List
  • This can be used to convert Stream into any Collection class like ArrayList, LinkedList, etc.
  • Note: In below example, instead of ArrayList, LinkedList can also be used

UsingCollectorsToCollection.java

package net.bench.resources.stream.to.list;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UsingCollectorsToCollection {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to List<String>
		List<String> names = nameStream
				.collect(Collectors.toCollection(ArrayList::new));


		// 3. print to console
		System.out.println(names);
	}
}

Output:

[Rajiv, Anbu, Santosh, Abdul, Lingaraj]

3. Using Stream’s forEach loop

  • Create an Object of type ArrayList
  • Iterate through Stream elements and add each element to previously created ArrayList
  • Finally, print ArrayList to console

UsingForEach.java

package net.bench.resources.stream.to.list;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class UsingForEach {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. create Object to type ArrayList<String>
		List<String> namesList = new ArrayList<String>();


		// 3. iterate and add to ArrayList
		nameStream.forEach(name -> namesList.add(name));


		// 4. print to console
		System.out.println(namesList);
	}
}

Output:

[Rajiv, Anbu, Santosh, Abdul, Lingaraj]

4. Convert Infinite Stream to List

  • Define IntStream with
    • starting value of 10 (seed)
    • then hopping with interval of 5 (function to produce next/new element)
    • limiting to 7 integer numbers (limit() method of Stream API)
  • Finally, print List to console

InfiniteStreamToList.java

package net.bench.resources.stream.to.list;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class InfiniteStreamToList {

	public static void main(String[] args) {

		// 1. define Infinite IntStream
		IntStream numberStream = IntStream.iterate(10, i -> i + 5);


		// 2. limit to 7 numbers and convert to List
		List<Integer> numberList = numberStream
				.limit(7)
				.boxed()
				.collect(Collectors.toList());


		// 3. print to console
		System.out.println(numberList);
	}
}

Output:

[10, 15, 20, 25, 30, 35, 40]

5. Convert Stream to an Array and then to List

  • First, convert Stream into an Array using toArray() method of Stream
  • Second, convert Array obtained from previous step to List using asList() method of java.util.Arrays
  • Finally, print List to console

ConvertStreamtoArrayAndThenToList.java

package net.bench.resources.stream.to.list;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class ConvertStreamtoArrayAndThenToList {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. First, convert Stream to Array
		String[] nameArray = nameStream.toArray(String[]::new); 


		// 3. Second, convert Array to List
		List<String> nameList = Arrays.asList(nameArray);


		// 4. print to console
		System.out.println(nameList);
	}
}

Output:

[Rajiv, Anbu, Santosh, Abdul, Lingaraj]

6. Processing Stream

  • Streams can be processed with any number of intermediate operations but result won’t be produced until final terminal operation is executed
    • Streams can be filtered to remove unwanted items before producing result into List
    • Stream elements can be mapped before producing result into List

6.1 Filter and convert to List

  • filter() method of Stream API accepts Predicate Functional Interface
  • Thus, passing predicate helps to filter unwanted elements and remaining elements collected into List using collect(Collectors.toList()); method

FilterStreamAndConvertToList.java

package net.bench.resources.stream.to.list;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FilterStreamAndConvertToList {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to List<String>
		List<String> names = nameStream
				.filter(name -> name.startsWith("A")) // filter
				.collect(Collectors.toList()); // collect to List


		// 3. print to console
		System.out.println("Names starting with A : \n\n" + names);
	}
}

Output:

Names starting with A : 

[Anbu, Abdul]

6.2 Map Stream elements and convert to List

  • map() method of Stream API accepts Function Functional Interface which transforms Stream elements from one form to another form
  • Finally, transformed Stream elements can be collected into List using collect(Collectors.toList()); method

MapStreamAndConvertToList.java

package net.bench.resources.stream.to.list;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MapStreamAndConvertToList {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to List<String>
		List<String> names = nameStream
				.map(name -> name.toUpperCase()) // mapping
				.collect(Collectors.toList()); // collect to List


		// 3. print to console
		System.out.println("Upper case names : \n\n" + names);
	}
}

Output:

Upper case names : 

[RAJIV, ANBU, SANTOSH, ABDUL, LINGARAJ]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - Convert List to Stream
Java 8 - Iterating List from JDK 1.0 to Java 1.8 version in 7 ways