collect()

A terminal stream operation that gathers results into a collection or other container.

What does collect() do in Java?

collect() — A terminal stream operation that gathers results into a collection or other container.

import java.util.List;

class NameFilter {
    List<String> shortNames(List<String> names) {
        return names.stream()
                .filter(name -> name.length() < 5)
                .map(String::toUpperCase)
                .toList();
    }
}

Level: Freelancer · More Java terms at this level

Updated