![]() |
VOOZH | about |
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.
Java program to implement a City class
We also create a helper method to prepare sample data:
toList() method transforms the input elements into a new and returns a Collector. Here,
Java program to implement the toList() method
Output:
๐ Collectors Example in Java[New Delhi, Mexico, New York, Dubai, London, Kolkata, Sydney, Mexico, Dubai]
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.
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]
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.
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}
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}
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]}
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
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]}