group list of complex object using java stream

Viewed 408

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",
      }
      
   ]
}
  1. group elements in the list by the field "callChargingId".
  2. 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...

1 Answers

Key point here is to implement BinaryOperator<U> mergeFunction object which will do the most complex part: merging two Map instances. I suggest to use a method with 3 parameters: toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator mergeFunction):

  1. keyMapper - callChargingId value,
  2. valueMapper - Map instance
  3. mergeFunction - merger which will merge two instances of type BinaryOperator<Map<String, Object>>

Simple implementation could look like this:

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.MapType;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

public class JsonTypeInfoApp {
    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        JsonMapper mapper = JsonMapper.builder()
                .enable(SerializationFeature.INDENT_OUTPUT)
                .build();
        MapType jsonType = mapper.getTypeFactory().constructMapType(Map.class, String.class, List.class);
        Map<String, List<Map<String, Object>>> response = mapper.readValue(jsonFile, jsonType);

        List<Map<String, Object>> gprsEvents = response.get("events");
        Map<Object, Map<String, Object>> result = gprsEvents.stream()
                .collect(Collectors.toMap(
                        map -> map.get("callChargingId"),
                        map -> map,
                        new EventMerger()));

        mapper.writeValue(System.out, result);
    }
}

class EventMerger implements BinaryOperator<Map<String, Object>> {

    @Override
    public Map<String, Object> apply(Map<String, Object> map0, Map<String, Object> map1) {
        map1.forEach((secondKey, secondValue) -> {
            map0.compute(secondKey, (key, value) -> {
                if (value == null) {
                    return secondValue;
                } else if (value instanceof Set) {
                    Set<Object> values = (Set<Object>) value;
                    values.add(secondValue);
                    return values;
                }

                Set<Object> values = new HashSet<>();
                values.add(value);
                values.add(secondValue);

                return values;
            });
        });
        return map0;
    }
}

Above code prints:

{
  "4100853125" : {
    "localTimeStamp" : [ "20170523113305", "20190523113305" ],
    "serviceCode" : [ "GPRS" ],
    "recEntityCode" : [ [ "1", "2" ], [ "2", "4" ] ],
    "index" : [ "1", "2" ],
    "dataVolumeOutgoing" : [ "47600", "27600" ],
    "callChargingId" : [ "4100853125" ],
    "dataVolumeIncoming" : [ "400000", "300000" ]
  },
  "5100853125" : {
    "localTimeStamp" : "20180523113305",
    "serviceCode" : "GPRS",
    "recEntityCode" : [ "1", "2" ],
    "index" : "7",
    "dataVolumeIncoming" : "100000",
    "dataVolumeOutgoing" : "17600",
    "callChargingId" : "5100853125"
  }
}
Related