List<LinkedHashMap<String, Object>> dateMapList map中的key为列名,value为数据,请将list中列名相同的key做合并,bigdecimal的类型用加法做合并,其他类型的默认取第一条数据
时间: 2024-05-10 18:16:47 浏览: 115
可以参考下面的代码实现:
```java
List<LinkedHashMap<String, Object>> resultList = new ArrayList<>();
Map<String, BigDecimal> sumMap = new HashMap<>();
Map<String, String> stringMap = new HashMap<>();
Map<String, Object> objectMap = new HashMap<>();
for (LinkedHashMap<String, Object> map : dateMapList) {
for (String key : map.keySet()) {
Object value = map.get(key);
if (value instanceof BigDecimal) {
BigDecimal sum = sumMap.get(key);
if (sum == null) {
sum = BigDecimal.ZERO;
}
sumMap.put(key, sum.add((BigDecimal)value));
} else if (value instanceof String) {
if (!stringMap.containsKey(key)) {
stringMap.put(key, (String)value);
}
} else {
if (!objectMap.containsKey(key)) {
objectMap.put(key, value);
}
}
}
}
for (LinkedHashMap<String, Object> map : dateMapList) {
LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
for (String key : map.keySet()) {
Object value = map.get(key);
if (value instanceof BigDecimal) {
resultMap.put(key, sumMap.get(key));
} else if (value instanceof String) {
resultMap.put(key, stringMap.get(key));
} else {
resultMap.put(key, objectMap.get(key));
}
}
resultList.add(resultMap);
}
```
这里首先遍历列表中的每个 map,对于每个 key-value 对,根据 value 的类型分别进行处理,BigDecimal 的值用一个 sumMap 做累加,String 的值用一个 stringMap 做去重,其他类型的值用一个 objectMap 做去重。
然后再次遍历列表中的每个 map,对于每个 key-value 对,根据 value 的类型分别从对应的 map 中取值并加入到结果 map 中。最后将结果 map 添加到结果列表中即可。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)