必须将HashMap列表分组为基于密钥具有相同时间戳的HashMap列表。
Map<String, Object> m0 = new HashMap<>();
m0.put("x", "aaa#0322");
m0.put("y", "saadaad");
m0.put("z", "7asasada89");
Map<String, Object> m1 = new HashMap<>();
m1.put("x", "bbb#0314");
m1.put("y", "asasadafa");
m1.put("z", "daasaada");
Map<String, Object> m2 = new HashMap<>();
m2.put("x", "bbb#0322");
m2.put("y", "asasdfaff");
m2.put("z", "2sadada22");
List<Map> l = new ArrayList<>(Arrays.asList(m0, m1, m2));
[{{x = aaa#0322,y = saadaad,z = 7asasada89},{x = bbb#0314,y = asasadafa,z = daasaada},{x = bbb#0322,y = asasdfaff,z = 2sadada22 },{x = bbb#0314,y = bsaadda,z = asfasaafe}]
Convert to a format where i can group it according to timestamp value in x like x#0322
[
[
{x=aaa#0322, y=saadaad, z=7asasada89}, {x=bbb#0322, y=asasdfaff, z=2sadada22}
],
[
{x=bbb#0314, y=asasadafa, z=daasaada}, {x=bbb#0314, y=bsaadda, z=asfasaafe}
]
]
Or Convert to a format where i can group it according to timestamp value in x like x#0322
[
{
{x=aaa#0322, y=saadaad, z=7asasada89}, {x=bbb#0322, y=asasdfaff, z=2sadada22}
},
{
{x=bbb#0314, y=asasadafa, z=daasaada}, {x=bbb#0314, y=bsaadda, z=asfasaafe}
}
]
答案 0 :(得分:2)
基本上,如果Map
包含键x
,则根据值进行分组
List<List<Map<String, String>>> result = l.stream()
.filter(m->m.containsKey("x")) // filter map with `x` key
.collect(Collectors.groupingBy(m->m.get("x").split("#")[1])) // if map contains `x` as key then group by the value after `#`
.entrySet().stream()
.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) //sort on requirement
.map(Entry::getValue)
.collect(Collectors.toList());
输出
[[{x=bbb#123, y=asasdfaff, z=2sadada22}], [{x=aaa#123, y=saadaad, z=7asasada89}], [{x=bbb#000, y=asasadafa, z=daasaada}]]