VOOZH about

URL: https://www.geeksforgeeks.org/java/java-collectors/

โ‡ฑ Java Collectors - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Collectors

Last Updated : 4 Dec, 2025

In Java, the Stream API provides a modern way to process data. But after filtering, mapping and transforming, we usually need to gather results into a collection, a map or even a single value.

Thatโ€™s where Collectors come in.

  • A Collector is an object that defines how to accumulate elements of a stream into a final result.
  • The Collectors utility class provides ready-to-use static methods, so you rarely need to write your own collectors.

Java program to implement a City class

We also create a helper method to prepare sample data:

Common Collectors Methods

1. toList()

toList() method transforms the input elements into a new and returns a Collector. Here,

Java program to implement the toList() method

Output:

[New Delhi, Mexico, New York, Dubai, London, Kolkata, Sydney, Mexico, Dubai]

๐Ÿ‘ Collectors Example in Java

2. toSet()

toSet() method transforms the input elements into a new Set and returns a Collector. This method will return

Java program to implement the toSet() method

Output:

[New York, New Delhi, London, Mexico, Kolkata, Dubai, Sydney]

Here, we can notice in the output that Mexico and Dubai have not been repeated.

3. toCollection(Supplier<C> collectionFactory)

toCollection(Supplier <C> collectionFactory) method collects stream elements into a user-defined collection, unlike toList() or toSet() which use defaults. C is the target collection type, and T is the element type.

Java program to implement the toCollection() method

Output:

[New Delhi, Mexico, New York, Dubai, London, Alaska, Kolkata, Sydney, Mexico, Dubai]

4. toMap()

toMap(Function keyMapper, Function valueMapper) transforms the elements into a Map

Java program to implement the Map() method

Output:

{New York=13.0, New Delhi=33.5, London=15.0, Mexico=14.0, Kolkata=30.0, Dubai=43.0, Sydney=11.0}

Binary operator specifies, how can we handle the collision. Above statements pick either of the colliding elements.

5. collectingAndThen()

Collector counting() method allows us to perform another operation on the result after collecting the input element of collection.

Java program to implement the collectingAndThen() method

Output:

{New York=1, New Delhi=1, London=1, Alaska=1, Mexico=2, Kolkata=1, Dubai=2, Sydney=1}

6. counting()

joining() method counts the number of input elements of type T and returns a Collector. This method is used in a case where we want to group and count the number of times each city is present in the collection of elements.

Java program to implement the counting() method

Output:

{New York=1, New Delhi=1, London=1, Alaska=1, Mexico=2, Kolkata=1, Dubai=2, Sydney=1}

7. groupingBy()

groupingBy(Function classifier) method performs group by operation on input elements of type T. The grouping of elements is done as per the passed classifier function and returns the Collector with result in a Map.

Java program to implement the groupingBy() method

Output:

{New York=[New York --> 13.0], New Delhi=[New Delhi --> 33.5], London=[London --> 15.0], Alaska=[Alaska --> 1.0], Mexico=[Mexico --> 14.0, Mexico --> 14.0], Kolkata=[Kolkata --> 30.0], Dubai=[Dubai --> 43.0, Dubai --> 43.0], Sydney=[Sydney --> 11.0]}

8. joining()

method concatenates the input elements into a String and returns a Collector.

1. Collector joining(CharSequence delimiter): Concatenates the input elements, separated by the specified delimiter, and returns a Collector.

Java program to implement th joining() method

Output:

New Delhi, Mexico, New York, Dubai, London, Kolkata, Sydney, Mexico, Dubai

2. Collector joining(): Concatenates the input elements, separated by the specified delimiter, as per the specified prefix and suffix, and returns a Collector.

Java program to implement the joining() methodExample:Javaimport java.util.ArrayList;

Output:

Prefix:New Delhi Mexico New York Dubai London Kolkata Sydney Mexico Dubai:Suffix

9. mapping()

Transforms a Collector of the input elements of type U to one the input elements of type T by applying a mapping function to every input element before the transformation.

Java program to implement the mapping() method

Output:

{New York=[13.0], New Delhi=[33.5], London=[15.0], Alaska=[1.0], Mexico=[14.0, 14.0], Kolkata=[30.0], Dubai=[43.0, 43.0], Sydney=[11.0]}

Comment
Article Tags: