繁体   English   中英

如何获得一年的总数

[英]How to get the total count in an year

获取每年的计数

这是我尝试过的

public static void main(String args[])
{       
    List<Map<String,Integer>> list_map = new ArrayList<Map<String,Integer>>();

    Map<String, Integer> map1 = new HashMap<String,Integer>();

    map1.put("Year",2018);
    map1.put("Month",1);
    map1.put("Cost",100);

    list_map.add(map1);

    Map<String,Integer> map2 = new HashMap<String,Integer>();

    map2.put("Year",2018);
    map2.put("Month",2);
    map2.put("Cost",200);

    list_map.add(map2);

    Map<String,Integer> map3 = new HashMap<String,Integer>();

    map3.put("Year",2018);
    map3.put("Month",3);
    map3.put("Cost",300);

    list_map.add(map3);

    Map<String,Integer> map4 = new HashMap<String,Integer>();

    map4.put("Year",2017);
    map4.put("Month",1);
    map4.put("Cost",400);

    list_map.add(map4);

    Map<String,Integer> map5 = new HashMap<String,Integer>();

    map5.put("Year",2017);
    map5.put("Month",2);
    map5.put("Cost",500);

    list_map.add(map5);

    Map<String,Integer> map6 = new HashMap<String,Integer>();

    map6.put("Year",2017);
    map6.put("Month",3);
    map6.put("Cost",300);

    list_map.add(map6);


    Iterator<Map<String,Integer>> iterator = list_map.iterator();

    while(iterator.hasNext())
    {           
         Map<String,Integer> year = iterator.next();

         Set<Entry<String,Integer>> entrySet = year.entrySet();

         for(Entry<String, Integer> entry : entrySet) 
            {                
                System.out.println("Key : " + entry.getKey() +" " + "\tValue : " + entry.getValue());
            }   
         System.out.println();       
    }

输出

Key : Month     Value : 1
Key : Year  Value : 2018
Key : Cost  Value : 100

Key : Month     Value : 2
Key : Year  Value : 2018
Key : Cost  Value : 200

Key : Month     Value : 3
Key : Year  Value : 2018
Key : Cost  Value : 300

Key : Month     Value : 1
Key : Year  Value : 2017
Key : Cost  Value : 400

Key : Month     Value : 2
Key : Year  Value : 2017
Key : Cost  Value : 500

Key : Month     Value : 3
Key : Year  Value : 2017
Key : Cost  Value : 300

预期产出

Year    Count
2018     600
2017     1200

我认为 Andronicus 的回答会很棒。 还有 Ali Azim,但我的变种更小。

Map<Integer,Integer> yearCounts = new HashMap<>();

for (Map<String, Integer> year : list_map) {

    if (yearCounts.containsKey(year.get("Year"))) {
        Integer count = yearCounts.get(year.get("Year"));
        yearCounts.put(year.get("Year"), year.get("Cost") + count);
    } else {
        yearCounts.put(year.get("Year"), year.get("Cost"));
    }
}

System.out.println("Year\tCount");

for (Map.Entry<Integer, Integer> yearCount : yearCounts.entrySet()) {
    System.out.println(yearCount.getKey() + "\t" + yearCount.getValue());
}

这是使用流的绝佳案例。

Map<Integer, Integer> yearStats = listMap.stream()
    .collect(Collectors.groupingBy(t -> t.get("Year"), Collectors.summingInt(t -> t.get("Cost"))));

解释:

使用stream方法,初始化流操作以遍历流的元素。 在这种情况下,每个元素都是一个Map 然后我们使用一个收集器,它根据地图的Year键的值进行分组,因此它将为我们提供每年的结果。 Collector.groupingBy方法的第二个参数允许我们传入另一个Collector 我们传入一个summingInt收集器,它从添加所有元素的流中收集元素。


顺便说一句,您使用Map而不是特定类有什么原因吗?

class A {
    int year;
    int month;
    int cost;
}

暂无
暂无

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

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