I think the problem is the new ObjStr2()
in your second collector. You’re using the same starting element for all groups.
You’re concatenating everything to the same object, and because this is mutable, everything is appended to that same object.
Instead, make your doReduce
return a new object:
static ObjStr2 doReduce(ObjStr2 a, ObjStr2 b) {
return new ObjStr2(a.str + b.str);
}
Output:
{1=[a], 2=[b, b, c, c], 3=[d, d, d]}
{1=abbccddd, 2=abbccddd, 3=abbccddd}
(But if you’re going to do that, you may as well just use String
directly).
solved Java Lambda groupby reducing with transformation [closed]