我是靠谱客的博主 碧蓝便当,最近开发中收集的这篇文章主要介绍List<Map<String,Object>>根据from和to多个字段去重,并且根据指定字段num求和,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
**//未处理数据前**
{sum=1134.0, from=8323544701622021896, to=8468946001390570465}
{sum=27.0, from=8323544701622021896, to=8468946001390570465}
{sum=11.0, from=8323544701622021896, to=8468946001390570465}
{sum=30.0, from=5743690555326549403, to=8468946001390570465}
{sum=36.0, from=8323544701622021896, to=8468946001390570465}
{sum=1291.0, from=8323544701622021896, to=8468946001390570465}
{sum=11.0, from=6357948542166894975, to=8468946001390570465}
{sum=19.0, from=8323544701622021896, to=8468946001390570465}
{sum=2.0, from=6357948542166894975, to=8468946001390570465}
**//处理数据后**
{sum=1134.0 + 27.0 + 36.0 + 1291.0 +19.0 + 32.0, from=8323544701622021896, to=8468946001390570465}
{sum=30.0, from=5743690555326549403, to=8468946001390570465}
{sum=13.0, from=6357948542166894975, to=8468946001390570465}
//创建测试数据
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> maps = new HashMap<>();
Map<String, Object> map1 = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
Map<String, Object> map4 = new HashMap<>();
Double a1 = 1134.0;
Double b = 27.0;
Double c = 30.0;
Double d = 11.0;
Double e = 81071.0;
maps.put("from", "8323544701622021896");
maps.put("to", "8468946001390570465");
maps.put("sum", a1);
map1.put("from", "8323544701622021896");
map1.put("to", "8468946001390570465");
map1.put("sum", b);
map2.put("from", "5743690555326549403");
map2.put("to", "8468946001390570465");
map2.put("sum", c);
map3.put("from", "6357948542166894975");
map3.put("to", "8468946001390570465");
map3.put("sum", d);
map4.put("from", "5509372442598951778");
map4.put("to", "8468946001390570465");
map4.put("sum", e);
list.add(maps);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
解析数据 第一种方法
Map<HashMap<String, String>, Double> mapMap = new HashMap<>();
for (Map<String, Object> map : list) {
String from = (String) map.get("from");
String to = (String) map.get("to");
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("from", from);
hashMap.put("to", to);
if (!mapMap.containsKey(hashMap)) {
mapMap.put(hashMap, (Double) map.get("sum"));
} else {
Double sum = mapMap.get(hashMap);
Double sum1 = (Double) map.get("sum");
mapMap.put(hashMap, sum + sum1);
}
}
System.out.println(mapMap.toString());
List<Map<String, Object>> edgesListMap = new ArrayList<>();
for (HashMap<String, String> hashMap : mapMap.keySet()) {
Map<String, Object> edgesMap = new HashMap<>(8);
edgesMap.put("sum", mapMap.get(hashMap));
edgesMap.put("from", hashMap.get("from"));
edgesMap.put("to", hashMap.get("to"));
edgesListMap.add(edgesMap);
}
方法中少new资源,所以第一种的解析数据可以替换
//需要替换的代码
List<Map<String, Object>> edgesListMap = new ArrayList<>();
for (HashMap<String, String> hashMap : mapMap.keySet()) {
Map<String, Object> edgesMap = new HashMap<>(8);
edgesMap.put("sum", mapMap.get(hashMap));
edgesMap.put("from", hashMap.get("from"));
edgesMap.put("to", hashMap.get("to"));
edgesListMap.add(edgesMap);
}
//替换后的代码
List<Map<String, Object>> list1 = new ArrayList<>();
Set<HashMap<String, String>> set = mapMap.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
HashMap<String, String> mapMapkey = (HashMap<String, String>) iter.next();
String from = mapMapkey.get("from");
String to = mapMapkey.get("to");
Double sum = mapMap.get(mapMapkey);
HashMap<String, Object> finalMap = new HashMap<>();
finalMap.put("from", from);
finalMap.put("to", to);
finalMap.put("sum", sum);
list1.add(finalMap);
}
第二种方法
//创建实体类 和main方法一个文件
class A {
private String from;
private Double num;
private String to;
public A() {
}
public A(String from, Double num, String to) {
this.from = from;
this.num = num;
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public Double getNum() {
return num;
}
public void setNum(Double num) {
this.num = num;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
第三种(解析数据)
List<A> inList = new ArrayList<>();
for (Map<String, Object> mapNodes : list) {
A a = new A();
a.setFrom(mapNodes.get("from").toString());
a.setTo(mapNodes.get("to").toString());
a.setNum((Double) mapNodes.get("sum"));
inList.add(a);
}
Map<String, A> map = new HashMap<>();
inList.forEach(item -> {
String key = item.getFrom() + ";" + item.getTo();
if (map.containsKey(key)) {
A a = map.get(key);
a.setNum(a.getNum() + item.getNum());
map.put(key, a);
} else {
map.put(key, item);
}
});
List<A> collect = map.values().stream().collect(Collectors.toList());
System.out.println(collect);
}
//群二维码
最后
以上就是碧蓝便当为你收集整理的List<Map<String,Object>>根据from和to多个字段去重,并且根据指定字段num求和的全部内容,希望文章能够帮你解决List<Map<String,Object>>根据from和to多个字段去重,并且根据指定字段num求和所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复