2

I need to count occurences of some special bundles.

Map<Integer,Integer> countBundles(){
    return bundles.stream()
        .bla()
        .bla()
        .bla()
        .collect(groupingBy(Bundle::getId), counting()));
}

This code does not compile because counting returns Long. Is there any beautiful way to return Map<Integer, Integer>?

I have this idea, but it`s ugly

  map.entrySet().stream()
     .collect(toMap(Map.Entry::getKey, entry -> (int) entry.getValue().longValue()));
artmmslv
  • 139
  • 3
  • 12
  • There should be no other way using the `Streams` API. Added to that, I would not agree that what you have tried so far is ugly, as that looks like the obvious way to do it following a functional stype. – tmarwen Dec 03 '20 at 10:49
  • would this link help you? https://stackoverflow.com/questions/51968025/count-elements-in-a-stream-and-return-integer-insted-of-long/51968144 – Mustafa Poya Dec 03 '20 at 11:00

2 Answers2

5

You can use Collectors.collectingAndThen to apply a function to the result of your Collector:

Map<Integer,Integer> countBundles() {
    return bundles.stream()
        .bla()
        .bla()
        .bla()
        .collect(groupingBy(Bundle::getId, collectingAndThen(counting(), Long::intValue)));
}

If you need some other semantics than just a cast, replace Long::intValue with some other conversion code.

Hoopje
  • 12,677
  • 8
  • 34
  • 50
3

There is no build-in way to use Collectors.counting() with Integer, since generics are invariant. However, you can write a custom Collector with minimal effort:

public static <T> Collector<T, ?, Integer> countingInt() {
    return Collectors.summingInt(e -> 1);
}

You can also use the plain summingInt(e -> 1) if you want to use native libraries only.

Example:

Map<Integer,Integer> countBundles(){
    return bundles.stream()
        .bla()
        .bla()
        .bla()
   // 1 .collect(groupingBy(Bundle::getId), countingInt()));
   // 2 .collect(groupingBy(Bundle::getId), summingInt(e -> 1)));
}

Please note that with signed interpretation, you can count at most 2.147.483.647 elements.

Glains
  • 2,773
  • 3
  • 16
  • 30