In this tutorial, we will see “How to convert a Stream to a List in Java 8?”

To achieve that, we are going to use Collectors.toList() operation available since Java 8.

Convert a Stream to a List in Java 8 with example...!!! Share on X

Example 1. With String

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

public class Convert_Stream_To_List_Java_8_Example_1 {

    public static void main(String[] args) {

        System.out.println("Example 1: (Basic example with String) - Convert Stream to List\n");

        Stream <String> persons = Stream.of("Tom", "John", "Peterson", "Tony", "Tim");

        //Convert Stream to List
        List <String> personList = persons.collect(Collectors.toList());

        //Below mentioned all three forms will generate the same result. You can use any of those options.

        //Printing list using Method Reference
        System.out.println("Displaying list using Method Reference\n");
        personList.forEach(System.out::println);

        //Printing list using Lambda Expression
        System.out.println("\nDisplaying list using Lambda Expression\n");
        personList.forEach(person -> System.out.println(person));

        //Printing list using Annonymous Inner Class
        System.out.println("\nDisplaying list using Annonymous Inner Class");
        personList.forEach(new Consumer <String> () {

            @Override
            public void accept(String person) {
                System.out.println(person);
            }
        });
    }

}

Output:

Example 1: (Basic example with String) - Convert Stream to List

Displaying list using Method Reference

Tom
John
Peterson
Tony
Tim

Displaying list using Lambda Expression

Tom
John
Peterson
Tony
Tim

Displaying list using Annonymous Inner Class
Tom
John
Peterson
Tony
Tim

Example 2. With Filtered String

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

public class Convert_Stream_To_List_Java_8_Example_2 {

    public static void main(String[] args) {

        System.out.println("Example 2: (with String but filtered) - Convert Stream to List\n");

        Stream <String> persons = Stream.of("Tom", "John", "Peterson", "Tony", "Tim");

        //Find person whose name starts with 'T' and then convert the Stream to List
        List <String> personList = persons
            .filter(person -> person.startsWith("T"))
            .collect(Collectors.toList());


        System.out.println("Displaying list using Method Reference\n");
        personList.forEach(System.out::println);

    }

}

Output:

Example 2: (with String but filtered) - Convert Stream to List

Displaying list using Method Reference

Tom
Tony
Tim

Example 3. With Integer

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

public class Convert_Stream_To_List_Java_8_Example_3 {

    public static void main(String[] args) {

        System.out.println("Example 3: (Basic example with Integer) - Convert Stream to List\n");

        Stream <Integer> numbers = Stream.of(10, 90, 43, 81, 25);

        //Convert Stream to List
        List <Integer> numberList = numbers.collect(Collectors.toList());

        //Below mentioned all three forms will generate the same result. You can use any of those options.

        //Printing list using Method Reference
        System.out.println("Displaying list using Method Reference\n");
        numberList.forEach(System.out::println);

        //Printing list using Lambda Expression
        System.out.println("\nDisplaying list using Lambda Expression\n");
        numberList.forEach(person -> System.out.println(person));

        //Printing list using Annonymous Inner Class
        System.out.println("\nDisplaying list using Annonymous Inner Class");
        numberList.forEach(new Consumer <Integer> () {

            @Override
            public void accept(Integer number) {
                System.out.println(number);
            }
        });
    }

}

Output:

Example 3: (Basic example with Integer) - Convert Stream to List

Displaying list using Method Reference

10
90
43
81
25

Displaying list using Lambda Expression

10
90
43
81
25

Displaying list using Annonymous Inner Class
10
90
43
81
25

Example 4. With Filtered Integer

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

public class Convert_Stream_To_List_Java_8_Example_4 {

    public static void main(String[] args) {

        System.out.println("Example 4: (with Integer but filtered) - Convert Stream to List\n");

        Stream <Integer> numbers = Stream.of(10, 90, 43, 81, 25);

        //Find even numbers from the stream and then convert the Stream to List
        List < Integer > numberList = numbers
            .filter(number -> number % 2 == 0)
            .collect(Collectors.toList());


        System.out.println("Displaying list using Method Reference\n");
        numberList.forEach(System.out::println);

    }

}

Output:

Example 4: (with Integer but filtered) - Convert Stream to List

Displaying list using Method Reference

10
90

Convert a Stream to a List in Java 8 with example...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts