See if you can't find Modern Java Recipes by Ken Kousen (O'Reilly, 2017) (page 281). He explains how a Collector object is built with three generic types:-
. . . the generic types are defined as:
• T , the type of input elements to the reduction operation
• A , the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
• R , the result of the reduction operation
That would appear to correspond to the Collector#of() method, but there is another of() method which looks easier to use. Be sure to read the documentation links, too. The first argument is a Supplier, whose functional method is get(). Let's imagine I want to write a Collector<Integer, Long, Long>.At this point I am probably going to get the whole thing wrong and Stephan will have to correct it for me
Anyway, you can see you have a λ representing a Supplier<Long>. One down, two to go.
You have declared the first argument as a Supplier<Long> and we have declared <A> as representing Long, and the middle functionparameter is a BiConsumer<A, T>, whose functional method is accept(). So we want something implementing the functional method of a BiConsumer<Long, Integer>. So let's tryI am sure you could use boxing and unboxing to reduce line 4 by about 90% of its length. Two down one to go.
Now, what happens with the third argument is that you might be multi‑threading (parallelStream() rather than ordinary stream()), so you get Long results from each of the different threads, and you have to combine them, which you do with a BinaryOperator<A, R> (whose functional method is apply(T, U). So what we are trying to do is multiply the two Longs from each Thread, so let's try this:-The fourth argument is called finisher, but I don't think we need any post‑processing, so let's try passing null. It is a Function<Long, Long>, whose functional method is apply(T), so alternatives would be l -> l or even “Function::identity” if you don't mind a method reference.I can't see that we would need any characteristics, so let's pass a 0‑length array, like this:-Now read the documentation for the of() method and work out what mistake I have made
I am adding this thread to a more general forum in the hope of getting you more attention.
Edits: Various spelling corrections and correct the <A> formal type parameter which was invisible (it is too similar to a common HTML tag). Change function to parameter. Leave my (two) mistakes not corrected