Java 8 - Convert Stream to List

Introduction

In Java 8, the Stream API allows for efficient processing of data collections like lists, arrays, and more. Often, after processing a stream, you might want to collect the result back into a list. Java 8 provides a straightforward way to convert a stream into a list using the collect() method with Collectors.toList().

In this guide, we will learn how to convert a stream into a list in Java 8.

Solution Steps

  1. Create or Obtain a Stream: Generate or obtain a stream from a collection or another data source.
  2. Process the Stream (Optional): Perform any filtering, mapping, or other stream operations as needed.
  3. Use collect(Collectors.toList()): Collect the elements of the stream into a list.
  4. Display or Use the List: Use or print the resulting list.

Java Program

Example 1: Convert Stream of Integers to a List

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

public class StreamToListExample {
    public static void main(String[] args) {
        // Step 1: Create a stream of integers
        Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);

        // Step 2: Convert the stream to a list
        List<Integer> numberList = numberStream.collect(Collectors.toList());

        // Step 3: Display the list
        System.out.println(numberList);  // Output: [1, 2, 3, 4, 5]
    }
}

Output

[1, 2, 3, 4, 5]

Explanation

Step 1: Create a Stream

We create a stream of integers using Stream.of():

Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);

Step 2: Convert the Stream to a List

We use collect(Collectors.toList()) to convert the stream into a list:

List<Integer> numberList = numberStream.collect(Collectors.toList());

Step 3: Display the List

The resulting list of integers is printed:

System.out.println(numberList);

Example 2: Convert Stream of Strings to a List

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

public class StreamOfStringsToList {
    public static void main(String[] args) {
        // Step 1: Create a stream of strings
        Stream<String> fruitStream = Stream.of("Apple", "Banana", "Orange", "Mango");

        // Step 2: Convert the stream to a list
        List<String> fruitList = fruitStream.collect(Collectors.toList());

        // Step 3: Display the list
        System.out.println(fruitList);  // Output: [Apple, Banana, Orange, Mango]
    }
}

Output

[Apple, Banana, Orange, Mango]

Explanation

Step 1: Create a Stream

We create a stream of strings using Stream.of():

Stream<String> fruitStream = Stream.of("Apple", "Banana", "Orange", "Mango");

Step 2: Convert the Stream to a List

We convert the stream into a list using collect(Collectors.toList()):

List<String> fruitList = fruitStream.collect(Collectors.toList());

Step 3: Display the List

We print the resulting list of strings:

System.out.println(fruitList);

Example 3: Convert a Filtered Stream to a List

In this example, we filter the stream of numbers and collect only the even numbers into a list.

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

public class FilteredStreamToList {
    public static void main(String[] args) {
        // Step 1: Create a stream of integers
        Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // Step 2: Filter and convert the stream to a list of even numbers
        List<Integer> evenNumbers = numberStream
            .filter(n -> n % 2 == 0)  // Filter even numbers
            .collect(Collectors.toList());  // Collect into a list

        // Step 3: Display the list of even numbers
        System.out.println(evenNumbers);  // Output: [2, 4, 6, 8, 10]
    }
}

Output

[2, 4, 6, 8, 10]

Explanation

Step 1: Create a Stream

We create a stream of integers:

Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Step 2: Filter the Stream and Convert to a List

We use filter() to retain only the even numbers, then collect them into a list:

List<Integer> evenNumbers = numberStream
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());

Step 3: Display the List

The filtered list of even numbers is printed:

System.out.println(evenNumbers);

Conclusion

In Java 8, converting a stream into a list is simple and efficient using the collect(Collectors.toList()) method. Whether you are working with integers, strings, or any other objects, you can easily gather the processed stream elements into a list. This makes the Stream API both powerful and flexible for various data processing tasks.

Comments