Java Stream toList() Method

The toList() method in Java, part of the java.util.stream.Stream interface, is used to collect the elements of a stream into an immutable list. This method is useful when you need to convert a stream into a list for further processing or manipulation.

Table of Contents

  1. Introduction
  2. toList() Method Syntax
  3. Understanding toList()
  4. Examples
    • Basic Usage
  5. Real-World Use Case
  6. Conclusion

Introduction

The toList() method is a terminal operation that collects the elements of a stream into an immutable list. This method is available starting from Java 16 and provides a convenient way to convert streams into lists.

toList() Method Syntax

List<T> toList()

Parameters:

  • This method does not take any parameters.

Returns:

  • An immutable List containing the elements of the stream.

Throws:

  • This method does not throw any exceptions.

Understanding toList()

The toList() method collects all elements of the stream into an immutable list. This method is useful for scenarios where you need a list representation of the stream elements.

Examples

Basic Usage

To demonstrate the basic usage of toList(), we will create a Stream of integers and collect them into a list.

Example

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

public class ToListExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);

        // Use toList() to collect elements into a list
        List<Integer> list = stream.toList();

        // Print the elements of the list
        list.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Real-World Use Case

Converting a Stream of Employees to a List

In real-world applications, the toList() method can be used to convert a stream of complex objects, such as employees, into a list for further processing.

Example

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

public class ToListRealWorldExample {
    static class Employee {
        String name;
        double salary;

        Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }

        @Override
        public String toString() {
            return name + ": " + salary;
        }
    }

    public static void main(String[] args) {
        Stream<Employee> employees = Stream.of(
            new Employee("Alice", 50000),
            new Employee("Bob", 60000),
            new Employee("Charlie", 70000)
        );

        // Use toList() to collect elements into a list
        List<Employee> employeeList = employees.toList();

        // Print the elements of the list
        employeeList.forEach(System.out::println);
    }
}

Output:

Alice: 50000.0
Bob: 60000.0
Charlie: 70000.0

Conclusion

The Stream.toList() method is used to collect the elements of a stream into an immutable list. This method is particularly useful for converting streams into lists for further processing or manipulation. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that elements are collected into lists as needed.

Comments