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
public class City {
private String name;
private double temperature;
// Parameterized constructor
public City(String name, double temperature) {
this.name = name;
this.temperature = temperature;
}
// Getters
public String getName() { return name; }
public Double getTemperature() { return temperature; }
// Overriding toString for display
@Override
public String toString() {
return name + " --> " + temperature;
}
}
We also create a helper method to prepare sample data:
private static List<City> prepareTemperature() {
List<City> cities = new ArrayList<>();
cities.add(new City("New Delhi", 33.5));
cities.add(new City("Mexico", 14));
cities.add(new City("New York", 13));
cities.add(new City("Dubai", 43));
cities.add(new City("London", 15));
cities.add(new City("Alaska", 1));
cities.add(new City("Kolkata", 30));
cities.add(new City("Sydney", 11));
cities.add(new City("Mexico", 14)); // duplicate
cities.add(new City("Dubai", 43)); // duplicate
return cities;
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature().stream()
.filter(f -> f.getTemperature() > 10)
.map(f -> f.getName())
.collect(Collectors.toList()));
}
}
Output:
[New Delhi, Mexico, New York, Dubai, London, Kolkata, Sydney, Mexico, Dubai]

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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.filter(f -> f.getTemperature() > 10)
.map(f -> f.getName())
.collect(Collectors.toSet()));
}
}
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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.map(f -> f.getName())
.collect(Collectors.toCollection(List::new)));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
// Here, the name and the temperature are defined as the type City
System.out.println(prepareTemperature()
.stream()
.filter(city -> city.getTemperature() > 10)
.collect(Collectors.toMap(
City::getName,
City::getTemperature,
(key, identicalKey) -> key)));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
// Collects the elements and counts the occurrences
System.out.println(prepareTemperature()
.stream()
.collect(Collectors.groupingBy(
City::getName,
Collectors.collectingAndThen(
Collectors.counting(),
f -> f.intValue()))));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.collect(Collectors.groupingBy(
City::getName,
Collectors.counting())));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.collect(Collectors.groupingBy(City::getName)));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
// Concatenate the collection with comma
System.out.println(prepareTemperature()
.stream()
.filter(city -> city.getTemperature() > 10)
.map(f -> f.getName())
.collect(Collectors.joining(", ")));
}
}
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;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.filter(city -> city.getTemperature() > 10)
.map(f -> f.getName())
.collect(Collectors.joining(" ",
"Prefix:", ":Suffix")));
}
}
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
System.out.println(prepareTemperature()
.stream()
.collect(Collectors.groupingBy(
City::getName,
Collectors.mapping(
City::getTemperature,
Collectors.toList()))));
}
}
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]}