Java Stream collect()

In this guide, you will learn about the Stream collect() method in Java programming and how to use it with an example.

1. Stream collect() Method Overview

Definition:

The Stream.collect() method is a terminal operation that transforms the elements of a stream into a different kind of result, e.g., a collection or an arbitrary value. It performs a mutable reduction operation on the elements of the stream.

Syntax:

<R, A> R collect(Collector<? super T, A, R> collector)

Parameters:

- collector: A Collector which specifies how the stream elements should be accumulated, possibly transforming the accumulated result, and returning it.

Key Points:

- The collect() method is a versatile method and can transform a stream's elements into various types of outcomes including collections like lists, sets, or maps.

- Since collect() is a terminal operation, it closes the stream after its execution.

- Common collectors are provided by the Collectors class, which includes utility methods like toList(), toSet(), joining(), and more.

- Custom collectors can be implemented using the Collector interface for more advanced use cases.

- If the stream is parallel, and the collector is concurrent, then the collection process will also be done in parallel.

2. Stream collect() Method - Collect to List Example

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

public class StreamCollectExample {
    public static void main(String[] args) {
        Stream<String> fruitsStream = Stream.of("Apple", "Banana", "Cherry", "Date", "Elderberry");

        // Using collect to gather stream elements into a list
        List<String> fruitsList = fruitsStream.collect(Collectors.toList());

        System.out.println(fruitsList);
    }
}

Output:

[Apple, Banana, Cherry, Date, Elderberry]

Explanation:

In the example, we have a stream of fruit names. We use the collect() method in conjunction with the Collectors.toList() collector to gather all the names into a List. The resulting list is then printed, demonstrating the use of collect() to produce a collection from a stream.

3. Stream collect() Method  - Collect to Set Example

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

public class SetExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("Apple", "Banana", "Cherry", "Apple");
        Set<String> set = stream.collect(Collectors.toSet());
        System.out.println(set); // Output: [Apple, Cherry, Banana]
    }
}

Output:

[Apple, Cherry, Banana]

4. Stream collect() Method  - Collect to Map Example

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

class Employee {
    private final String name;
    private final int id;

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

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}

public class MapExample {
    public static void main(String[] args) {
        Stream<Employee> stream = Stream.of(new Employee("John", 1), new Employee("Jane", 2));
        Map<Integer, String> map = stream.collect(Collectors.toMap(Employee::getId, Employee::getName));
        System.out.println(map); // Output: {1=John, 2=Jane}
    }
}

Output:

{1=John, 2=Jane}

Comments