I have a list of gprsEvents where each element in the list is a Map<String,Object> as shown below. I need to:
{
"events":[
{
"localTimeStamp":"20170523113305",
"serviceCode":"GPRS",
"recEntityCode":[
"1",
"2"
],
"index":"1",
"dataVolumeIncoming":"400000",
"dataVolumeOutgoing":"27600",
"callChargingId":"4100853125",
},
{
"localTimeStamp":"20190523113305",
"serviceCode":"GPRS",
"recEntityCode":[
"2",
"4"
],
"index":"2",
"dataVolumeIncoming":"300000",
"dataVolumeOutgoing":"47600",
"callChargingId":"4100853125",
},
{
"localTimeStamp":"20180523113305",
"serviceCode":"GPRS",
"recEntityCode":[
"1",
"2"
],
"index":"7",
"dataVolumeIncoming":"100000",
"dataVolumeOutgoing":"17600",
"callChargingId":"5100853125",
}
]
}
- group elements in the list by the field "callChargingId".
- the result of the grouping should be a Map where the keys are the "callChargingIds", be one single element, where each of the fields containing a Set of fields as result of the grouping. The result should be as you can see below:
{
"5100853125":[
{
"localTimeStamp":"20180523113305",
"serviceCode":"GPRS",
"recEntityCode":[
"1",
"2"
],
"index":"7",
"dataVolumeIncoming":"100000",
"dataVolumeOutgoing":"17600"
}
],
"4100853125":[
{
"localTimeStamp":[
"20170523113305",
"20190523113305"
],
"serviceCode":"GPRS",
"recEntityCode":[
[
"1",
"2"
],
[
"2",
"4"
]
],
"index":[
"1",
"2"
],
"dataVolumeIncoming":"700000",
"dataVolumeOutgoing":"75200"
}
]
}
where for some fields the result of the grouping should be a list (timeStamp, recEntityCode and index), and in other fields the result should be the summe (dataVolumeIncoming and dataVolumeOutgoing
I started thinking about using java 8 stream (groupingBy): gprsEvents.stream().collect(Collectors.groupingBy(map -> map.get("callChargingId").toString()))
I'm now stuck to get suitable result specially to get the result in one single Map and the list or sum of the above mentioned fields...