To generate a Map
of type Map<OfficeType,Integer>
you can either use three-args version of Collector toMap()
, or a combination of Collectors collectiongAndThen()
and groupingBy()
+ collector counting()
as a downstream of grouping.
toMap()
If you're not expecting a large amount of elements having the same id
, you can use the following single-collector solution based on three-args toMap()
:
List<Booking> bookingList = bookingRepository.findByStatus(Status.APPROVED);
Map<OfficeType, Integer> officeTypeMap = bookingList.stream()
.collect(Collectors.toMap(
OfficeType::getId,
i -> 1,
Integer::sum
));
Note regarding performance:
There can be a small overhead caused generating new instances of Integer
(because code above operates with wrapper type and primitive int
). But all instances of Integer
having a value of less than 128
are cached by the JVM (i.e. they would be created only once, and then the same reference would be used). So if there's roughly a hundred of elements having identical id
there would be no tangible difference between this single collector-solution and the one listed below.
collectiongAndThen() & groupingBy()
This approach groupingBy()
in conjunction with counting()
as a downstream might be more performant when number if the data is massive. In this case, accumulation through collector counting()
, which calculates resulting value using primitive long
, can more advantages.
To transform the Long
into Integer
we can wrap groupingBy()
with collectingAndThen()
.
List<Booking> bookingList = bookingRepository.findByStatus(Status.APPROVED);
Map<OfficeType, Integer> officeTypeMap = bookingList.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(OfficeType::getId, Collectors.counting()),
Long::intValue)
));