hello I have a list of objects my object have three fields
class MyObject{
String x;
String y;
int z;
//getters n setters
}
I need to convert this list into a Map<String,Map<String,Integer>>
that is like this:
{x1:{y1:z1,y2:z2,y3:z3},x2{y4:z4,y5:z5}} format I want to do this in Java 8 which I think I am relatively new to it.
I have tried the following :
Map<String,Map<String,Integer>> map=list.stream().
collect(Collectors.
groupingBy(MyObject::getX,list.stream().
collect(Collectors.groupingBy(MyObject::getY,
Collectors.summingInt(MyObject::getZ)))));
this does not even compile. help is much appreciated
MyObject.xand the third and forth elements and map them using the second elements x value. It might be possible with a custom collector, but you are manipulating a lot of state