I really want to like Java 8 streams, but they seem overly verbose for things that seem like they should be simpler.
For instance, say you just want a sum of a collection of Integer:
void foo(Map barCounts) { // ... int total =
barCounts.values()
.stream()
.mapToInt(Integer::intValue)
.sum(); // ...}
The mapToInt() is necessary because sum() isn't available on a Stream . There's no implementation of mapToInt() without a parameter so you must pass something specific to unbox the Integer. You could use stream().reduce(0, Integer::sum) but that will cause the partial int sums that produce the final total to be boxed as Integer so that's necessarily less efficient.
None of these methods is satisfyingly short. It doesn't help that I understand why these drawbacks exist, they still manage to disappoint in some way.
For instance, say you just want a sum of a collection of Integer:
void foo(Map
barCounts.values()
.stream()
.mapToInt(Integer::intValue)
.sum(); // ...}
The mapToInt() is necessary because sum() isn't available on a Stream
None of these methods is satisfyingly short. It doesn't help that I understand why these drawbacks exist, they still manage to disappoint in some way.
Comments
Post a Comment