我有以下地图列表
List<Map<String, Object>> listBeforeGroup = new ArrayList<Map<String, Object>>();
Map<String, Object> m1 = new HashMap<String, Object>();
m1.put("company", "LG");
m1.put("billType", "A");
m1.put("billPeriod", "09-2018");
Map<String, Object> m2 = new HashMap<String, Object>();
m2.put("company", "LG");
m2.put("billType", "A");
m2.put("billPeriod", "09-2018");
Map<String, Object> m3 = new HashMap<String, Object>();
m3.put("company", "LG");
m3.put("billType", "A");
m3.put("billPeriod", "09-2018");
Map<String, Object> m4 = new HashMap<String, Object>();
m4.put("company", "LG");
m4.put("billType", "B");
m4.put("billPeriod", "01-2019");
Map<String, Object> m5 = new HashMap<String, Object>();
m5.put("company", "LG");
m5.put("billType", "B");
m5.put("billPeriod", "01-2019");
Map<String, Object> m6 = new HashMap<String, Object>();
m6.put("company", "Samsung");
m6.put("billType", "A");
m6.put("billPeriod", "10-2018");
Map<String, Object> m7 = new HashMap<String, Object>();
m7.put("company", "Samsung");
m7.put("billType", "A");
m7.put("billPeriod", "10-2018");
Map<String, Object> m8 = new HashMap<String, Object>();
m8.put("company", "Samsung");
m8.put("billType", "B");
m8.put("billPeriod", "11-2018");
listBeforeGroup.add(m1);listBeforeGroup.add(m2);
listBeforeGroup.add(m3);listBeforeGroup.add(m4);
listBeforeGroup.add(m5);listBeforeGroup.add(m6);
如何获得此输出?
//Desired Output - List<Map<String, Object>>
//{company=LG, billType=A, billPeriod=09-2018, count=3}
//{company=LG, billType=B, billPeriod=01-2019, count=2}
//{company=Samsung, billType=A, billPeriod=10-2018, count=2}
//{company=Samsung, billType=B, billPeriod=11-2018, count=1}
我使用Java 8流尝试了此操作,但无法获得所需的输出
List<Map<String, Object>> listAfterGroup = listBeforeGroup.stream().map(m -> m.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p - > p.getValue()))).collect(Collectors.toList());
并尝试了此方法,顺便说一句,此解决方案给出了一张地图,但我不希望这样做
Map<Object, Long> listAfterGroup = listBeforeGroup.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey,Collectors.counting()));
例如,我想按“ billPeriod”键对地图进行分组,并按值对项目计数,然后生成新的地图列表。
答案 0 :(得分:4)
您可以创建一个类sms:<phone number>, e.g. sms:5550101234 Send an SMS message to <phone
number> using the default messaging app
,随后的操作将变得更加简单。
Company
初始化列表:
class Company {
String company;
String billType;
String billPeriod;
public Company(String company, String billType, String billPeriod) {
this.company = company;
this.billType = billType;
this.billPeriod = billPeriod;
}
// getters, setters, toString, etc
}
现在举个例子,您可以按公司名称分组:
List<Company> list = new ArrayList<>();
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "B", "01-2019"));
list.add(new Company("LG", "B", "01-2019"));
list.add(new Company("Samsung", "A", "10-2018"));
list.add(new Company("Samsung", "A", "10-2018"));
list.add(new Company("Samsung", "B", "11-2018"));
现在,执行所需的其他操作变得容易得多。此外,在这里您没有创建 8张地图,而只处理了 1个列表。
答案 1 :(得分:2)
对地图进行分组和计数真的很困难,因为增加计数器值后,地图数据将被更改。因此,您必须保存地图的原始数据,并将计数器值保存到另一个地图。计数过程完成后,加入2张地图。
Map<Map<String, Object>, Long> counterData = listBeforeGroup.stream().collect(Collectors.groupingBy(m -> m, Collectors.counting()));
List<Map<String, Object>> listAfterGroup = new ArrayList<>();
for (Map<String, Object> m : counterData.keySet()) {
Map<String, Object> newMap = new HashMap<>(m);
newMap.put("count", counterData.get(m));
listAfterGroup.add(newMap);
}
更新Java 8方法,不容易调试
List<Map<String, Object>> listAfterGroup = listBeforeGroup.stream().collect(Collectors.groupingBy(m -> m, Collectors.counting())).entrySet().stream().map(e -> {
Map<String, Object> newMap = e.getKey();
newMap.put("count", e.getValue());
return newMap;
}).collect(Collectors.toList());
答案 2 :(得分:0)
我使用Java 8的方法:
Function<Map<String, Object>, String> createHashKey = map ->
map.values().stream()
.map(val -> String.valueOf(val))
.collect(Collectors.joining());
BinaryOperator<Map<String, Object>> mergeDuplicate = (map1, map2) -> {
int incrementedCount = (int)map1.get("count") + 1;
map1.put("count", incrementedCount);
return map1;
};
List<Map<String, Object>> listAfterGroup = listBeforeGroup.stream()
.collect(Collectors.toMap(createHashKey, el -> {
el.put("count", 1);
return el;
},mergeDuplicate))
.values().stream()
.collect(toList());
也许不是最简洁的解决方案,但我认为它非常易读并且易于遵循代码的逻辑。