简体   繁体   English

如何对Java中不同键的哈希映射列表中的列值求和

[英]How to sum a column value in a list of hashmaps against different Keys in java

I have a List of HashMaps that are 我有一个HashMaps列表

1. Key/value             key/Value
   CODE=1                TOTAL=10

2. Key/value             key/Value
   CODE=1                TOTAL=10

3. Key/value             key/Value
   CODE=2                TOTAL=10

4. Key/value             key/Value
   CODE=2                TOTAL=10

5. Key/value             key/Value
   CODE=3                TOTAL=10

I want to get SUM total of the TOTAL columns using CODE as key in java for loop etc 我想使用CODE作为Java循环中的键来获取TOTAL列的总和

ie

iterate over the list for each key, show sum (added together) total 遍历每个键的列表,显示总和(加在一起)

console output should be 控制台输出应为

key with CODE 1 has total of 20 as sum
key with CODE 2 has total of 20 as sum
key with CODE 3 has total of 10 as sum

I have tried by using 我已经尝试过使用

int total = 0;
String previousCodeValue = "";
for(int i = 0; i < mapsList ; i++)
{
  Map map = mapsList.get(i);

  if(map.get("CODE") != previousCodeValue && !previousCodeValue.equals("") )
  {
     system.out.print(total);
     total = 0;
  }
    previousCodeValue = map.get("CODE") ;
    total = total + map.get("TOTAL") 
 }

The issue is the loops runs fine as long as CODE remains changing, but on the last code value, the logic will not work :( what to do ? 问题是只要CODE保持更改,循环就可以正常运行,但是在最后一个代码值上,逻辑将不起作用:(怎么办?

Regards, 问候,

Aiden 艾登

Try to use Iterator like this: 尝试像这样使用Iterator

    int totale = 0;
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry tot = (Map.Entry)it.next();
    totale +=  tot.getValue();
    }

It appears all you are missing is outputing the last total after the loop is done : 似乎您所缺少的只是在循环完成后输出最后一个总数:

int total = 0;
String previousCodeValue = "";
for(int i = 0; i < mapsList ; i++)
{
  Map map = mapsList.get(i);

  if(map.get("CODE") != previousCodeValue && !previousCodeValue.equals("") )
  {
     System.out.print(total);
     total = 0;
  }
    previousCodeValue = map.get("CODE") ;
    total = total + map.get("TOTAL") 
 }
 System.out.print(total);

So if I understand correctly you need the SUM of the TOTAL values for each unique KEY over all the maps. 因此,如果我正确理解,则需要所有地图上每个唯一KEY的TOTAL值的总和。 Supposedly you would want to do this in one iteration so you need to keep track of unique keys and totals as you go along. 假设您希望在一次迭代中执行此操作,因此在进行过程中需要跟踪唯一键和总计。 Below I keep track of the results in a map (yes, another map, it's just an example). 下面,我在一张地图中跟踪结果(是的,另一张地图,仅是一个示例)。

private static void main(String[] args) {
    printResults(determineResults(createMaps()));
}

private static List<Map<String, Integer>> createMaps() {
    List<Map<String, Integer>> maps = new ArrayList<>();
    maps.add(createMap(1, 10));
    maps.add(createMap(1, 10));
    maps.add(createMap(2, 10));
    maps.add(createMap(2, 10));
    maps.add(createMap(3, 10));
    return maps;
} 

private static Map<String, Integer> createMap(int code, int total) {
    Map<String, Integer> map = new HashMap<>();
    map.put("CODE", code);
    map.put("TOTAL", total);
    return map;
}

private static Map<Integer, Integer> determineResults(List<Map<String, Integer>> maps) {
    Map<Integer, Integer> results = new HashMap<>();
    for(Map<String, Integer) map : maps) {
        Integer key = map.get("CODE");
        Integer sum = results.get(key);
        if (sum == null) {
            sum = 0;
        }
        Integer total = map.get("TOTAL");
        sum += total;
        results.put(key, sum);
    }
    return results;
}

private static void printResults(Map<Integer, Integer> results) {
    for(Map.Entry<Integer, Integer> result : results) {
        System.out.printf("key with CODE %s has total of %s as sum", result .getKey(), result.getValue());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM