Java Collectors toList() Method

The toList() method in Java, part of the java.util.stream.Collectors class, is used to collect the elements of a stream into a List. This method is useful when you need to gather the elements of a stream into a list.

Table of Contents

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

Introduction

The toList() method returns a Collector that accumulates the input elements into a new List. This method is particularly useful for converting streams of data into lists for further processing or analysis.

toList() Method Syntax

The syntax for the toList() method is as follows:

public static <T> Collector<T, ?, List<T>> toList()

Parameters:

  • This method does not take any parameters.

Returns:

  • A Collector that collects the input elements into a List.

Throws:

  • This method does not throw any exceptions.

Understanding toList()

The toList() method allows you to collect the elements of a stream into a List. The resulting list is a mutable list, typically an ArrayList, that can be used for further processing or analysis.

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.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ToListExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        // Collect the numbers into a list
        List<Integer> numberList = numbers.stream()
                                          .collect(Collectors.toList());

        System.out.println("Number List: " + numberList);
    }
}

Output:

Number List: [1, 2, 3, 4, 5]

Using toList() with Custom Objects

This example shows how to use toList() with a stream of custom objects to collect them into a list.

Example

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

public class ToListCustomObjectExample {
    static class Product {
        String name;
        double price;

        Product(String name, double price) {
            this.name = name;
            this.price = price;
        }

        @Override
        public String toString() {
            return name + " ($" + price + ")";
        }
    }

    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
            new Product("Product A", 10.0),
            new Product("Product B", 20.5),
            new Product("Product C", 15.8)
        );

        // Collect the products into a list
        List<Product> productList = products.stream()
                                            .collect(Collectors.toList());

        System.out.println("Product List: " + productList);
    }
}

Output:

Product List: [Product A ($10.0), Product B ($20.5), Product C ($15.8)]

Real-World Use Case

Collecting Employee Names into a List

In real-world applications, the toList() method can be used to collect the names of employees into a list for further processing.

Example

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

public class EmployeeNameListExample {
    static class Employee {
        String name;
        int age;

        Employee(String name, int age) {
            this.name = name;
            this.age = age;
        }

        String getName() {
            return name;
        }
    }

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Alice", 30),
            new Employee("Bob", 25),
            new Employee("Charlie", 35)
        );

        // Collect the employee names into a list
        List<String> nameList = employees.stream()
                                         .map(Employee::getName)
                                         .collect(Collectors.toList());

        System.out.println("Name List: " + nameList);
    }
}

Output:

Name List: [Alice, Bob, Charlie]

Conclusion

The Collectors.toList() method is used to collect the elements of a stream into a List. This method is particularly useful for converting streams of data into lists for further processing or analysis. By understanding and using this method, you can efficiently manage collection operations in your Java applications.

Comments