posted 2 years ago
I find your code extremely confusing Piet. Please don't use existing type names for custom types. The name Short to me means java.lang.Short, which is the primitive wrapper type for the short integral type. It took me a while to realize it was a kind of type alias for a Map instead.
I'm also not a fan of declaring variables to hold functional types. Instead, just write a private method instead of a lambda expression and use a method reference.
Don't use method references or lambdas to refer to a method of a functional type. Pass the function object directly. In your code, you use sup::get in collect(), while passing sup directly will suffice.
Note that your collection operation can be replaced by just the following:
Or, if you want improved performance at the cost of losing encounter order:
The second option still maintains correct ordering of the keys, so you can still use lastEntry() to get the list of maximum elements, the list itself just won't maintain encounter order.
Now, if I didn't have access to the groupingBy() collector, I would write the code like this:
As you can see, the accumulate() and combine() methods closely resemble your consumers, except I made them into methods on a local Accumulation class.
This collector can easily be used in your main method and gives the exact same results as your original code: