java - java8 Stream List<Map> how to covert a Map after groupingBy -


i have data select mysql. want format it. data like:

var json = [    {      "date": "2016-01",      "number": "1",      "code": "420000",      "type": "false"    },    {      "date": "2016-01",      "number": "2",      "code": "440000",      "type": "false"    },    {      "date": "2016-02",      "number": "3",      "code": "420000",      "type": "false"    },    {      "date": "2016-03",      "number": "4",      "code": "420000",      "type": "true"    },    {      "date": "2016-03",      "number": "5",      "code": "410000",      "type": "false"    },    {      "date": "2016-03",      "number": "6",      "code": "440000",      "type": "true"    },    {      "date": "2016-04",      "number": "7",      "code": "420000",      "type": "false"    },    {      "date": "2016-04",      "number": "8",      "code": "440000",      "type": "false"    }  ];  console.log(json);

i want groupingby date, , partitioningby type, , reducing map. after format data like

var json = {    "2016-01": {      "false": {        "420000": "1",        "440000": "2"      }    },    "2016-02": {      "false": {        "420000": "3"      }    },    "2016-03": {      "false": {        "410000": "5"      },      "true": {        "420000": "4",        "440000": "6"      }    },    "2016-04": {      "false": {        "420000": "7",        "440000": "8"      }    }  };  console.log(json);

i coding java8 stream

public static void main(string[] args) {     list<map<string, string>> postmans = new arraylist<>();      // "420000" : post area code, "false": paper(without goods)     postmans.add(createmap("2016-01", "1", "420000", "false"));     postmans.add(createmap("2016-01", "2", "440000", "false"));     postmans.add(createmap("2016-02", "3", "420000", "false"));     postmans.add(createmap("2016-03", "4", "420000", "true"));     postmans.add(createmap("2016-03", "5", "410000", "false"));     postmans.add(createmap("2016-03", "6", "440000", "true"));     postmans.add(createmap("2016-04", "7", "420000", "false"));     postmans.add(createmap("2016-04", "8", "440000", "false"));      // before, use fastjson library     system.out.println(jsonobject.tojsonstring(postmans));       map<string, map<boolean, map>> data = postmans.stream()             .collect(collectors.groupingby(d -> d.get("date"), treemap::new,                     collectors.partitioningby(d-> d.get("type").equals("true"),                             collectors.reducing(new hashmap(), (d1, d2)->{                                 object code = d2.get("code");                                 object number = d2.get("number");                                 // think bug in reducing                                 d1.put(code, number);                                 return d1;                             }))));     // after, use fastjson library     system.out.println(jsonobject.tojsonstring(data));  }  private static map<string,string> createmap(string date, string number, string code, string type){     map<string, string> map = new hashmap<>();     map.put("date", date);     map.put("number", number);     map.put("code", code);     map.put("type", type);     return map; } 

but, real result is

var json = {    "2016-01": {      "false": {        "410000": "5",        "420000": "7",        "440000": "8"      },      "true": {        "410000": "5",        "420000": "7",        "440000": "8"      }    },    "2016-02": {      "false": {        "410000": "5",        "420000": "7",        "440000": "8"      },      "true": {        "410000": "5",        "420000": "7",        "440000": "8"      }    },    "2016-03": {      "false": {        "410000": "5",        "420000": "7",        "440000": "8"      },      "true": {        "410000": "5",        "420000": "7",        "440000": "8"      }    },    "2016-04": {      "false": {        "410000": "5",        "420000": "7",        "440000": "8"      },      "true": {        "410000": "5",        "420000": "7",        "440000": "8"      }    }  };  console.log(json);

i think cause of bug in reduing. when partitioningby new list <map>, old d1 still exists.

i want partitioningby result, each list <map> compressed map, can guarantee key (code) unique.

i sorry code long , english poor. thank me!

there 2 main mistakes here. collectors.reducing, since reduction, needs return new instance time , second logic flawed:

map<string, map<boolean, map<string, string>>> data = postmans.stream()             .collect(collectors.groupingby(                     d -> d.get("date"),                     treemap::new,                     collectors.partitioningby(                             d -> d.get("type").equals("true"),                             collectors.reducing(                                     new hashmap<>(),                                     (left, right) -> {                                          map<string, string> map = new hashmap<>();                                          string leftcode = left.get("code");                                         string leftnumber = left.get("number");                                          if (leftcode == null) {                                             map.putall(left);                                         } else {                                             map.put(leftcode, leftnumber);                                         }                                          string rightcode = right.get("code");                                         string rightnumber = right.get("number");                                          map.put(rightcode, rightnumber);                                          return map;                                      })))); 

you have problem of storing date string , doing treemap::new - sorts stringss, not sorting want. in general entire approach of having string makes code hardly readable.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -