Java Stream collect() Example

1. Introduction

This tutorial demonstrates the collect() method in the Java Stream API. The collect() method is a versatile mechanism for transforming stream elements into different kinds of result containers, such as lists, sets, or maps. It's a powerful tool for gathering results from stream operations.

Key Points

1. collect() is used to transform and gather elements from a stream into a collection or other types of result containers.

2. It typically requires a Collector, which specifies the mechanisms for reduction.

3. Common use cases include collecting elements into lists, sets, or maps, and performing string joins or grouping data.

2. Program Steps

1. Create Streams of elements.

2. Apply various collect() methods to transform these elements into different types of collections.

3. Demonstrate advanced uses of collect() like grouping elements or concatenating them.

3. Code Program

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

public class StreamCollectExample {

    public static void main(String[] args) {
        // Stream of strings
        Stream<String> stringStream = Stream.of("apple", "banana", "cherry", "date", "elderberry");

        // Collect to List
        List<String> list = stringStream.collect(Collectors.toList());
        System.out.println("List: " + list);

        // Stream of strings for Set
        Stream<String> stringStreamForSet = Stream.of("apple", "banana", "banana", "cherry");

        // Collect to Set
        Set<String> set = stringStreamForSet.collect(Collectors.toSet());
        System.out.println("Set: " + set);

        // Stream of strings for Map
        Stream<String> stringStreamForMap = Stream.of("apple", "banana", "cherry");

        // Collect to Map
        Map<String, Integer> map = stringStreamForMap.collect(Collectors.toMap(s -> s, String::length));
        System.out.println("Map: " + map);

        // Stream for joining strings
        Stream<String> stringStreamForJoining = Stream.of("apple", "banana", "cherry");

        // Collect to join strings
        String result = stringStreamForJoining.collect(Collectors.joining(", "));
        System.out.println("Joined string: " + result);
    }
}

Output:

List: [apple, banana, cherry, date, elderberry]
Set: [banana, cherry, apple]
Map: {apple=5, banana=6, cherry=6}
Joined string: apple, banana, cherry

Explanation:

1. Stream.of("apple", "banana", "cherry", "date", "elderberry") creates a stream of strings.

2. stringStream.collect(Collectors.toList()) collects the stream elements into a list.

3. Stream.of("apple", "banana", "banana", "cherry") demonstrates a stream where duplicates are included.

4. stringStreamForSet.collect(Collectors.toSet()) collects elements into a set, removing duplicates.

5. Stream.of("apple", "banana", "cherry") used for mapping demonstrates creating a map from the stream.

6. stringStreamForMap.collect(Collectors.toMap(s -> s, String::length)) creates a map where each fruit name maps to its length.

7. Stream.of("apple", "banana", "cherry") also demonstrates string joining.

8. stringStreamForJoining.collect(Collectors.joining(", ")) joins stream elements into a single string with a comma separator.

Comments