【问题标题】:How to calculate a value for each key of a HashMap?如何计算 HashMap 的每个键的值?
【发布时间】:2015-11-28 23:14:18
【问题描述】:

有什么方法可以在每次使用累加器后将其设置为 0,然后它仍然执行相同的功能?或如何计算 Map 中每个键的整体值,如下所示:String、Hashmap String、Integer?我试图尽可能澄清,但我对此一无所知。

/*EXAMPLE OF HASHMAP:  String e.g - UK ; America ; Africa , etc. String e.g - black , white, asian Integer e.g- 2008 , 103432 , 2391 // for every country the values are diff
*/
//one way to think of this is maybe with accumulator
// THIS IS AN EXAMPLE CODE WHICH DOES NOTHING.
int acc = 0 ;
for ( int i = 0 ; i < 20 ; i++ ) {
if ( i == 1 ) {
acc++;  }       // after acc ++  and it is 1, I want it to be again 0;
if ( i == 3 ) {
acc++;  }       // so that on this stage it will be 1
}

//Another alternative way may be:
Map<String,HashMap<String,Integer>> question = new HashMap<>();
int count = 0 ;
for(String regions : question.keySet())
{
for (String people : question.get(regions).keySet())
{
count = count + question.get(regions).get(people);  // and here it will count all the value but I want to count it for each region and then become to 0 again, so that I can have the overall value for each of the people(keys)
}
}

【问题讨论】:

  • ...不明白你的意思。您可以在使用它执行任何功能之后只 acc = 0 吗?请澄清您的查询。您确定不想在循环中包含第二个 if 吗?因为您当前的代码中不包括在内。而且循环没有意义。也许如果你能指定你想要完成的事情和问题,你可能会得到更好的帮助。
  • 如果你想让 act 保持在 0 那么增加它有什么意义呢?
  • 你忘记打开 { 括号 for 循环和右括号 } 因此我超出范围
  • 这里我没有给出具体的代码,而是一个示例。关键是计算 HashMap 中一个键的多个值(在我的情况下),然后移动到下一个键并再次计数,以便我拥有每个键的整体整数值。
  • 你的 hashmap 是什么样子的?

标签: java hashmap accumulator


【解决方案1】:

您的问题有点不清楚,但是,这是一个计算哈希图中键的值的示例。假设我们有

US -> [X -> 55, Y -> 5 , Z -> 10, ...] as one object
UK -> [X -> 99, Y -> 1, Z -> 50, W-> 23, ...] as other object

其中 X、Y 和 Z 是 String 类型,它们的值是 Integer 类型。以下代码存储上述结构,然后计算美国和英国的 X、Y、Z。

 //structure to store a map of key/value for a key
 Map<String, Map<String, Integer>> hashmap = new HashMap<String, Map<String, Integer>>(); 

   //key/value map
   Map<String, Integer> US = new HashMap<String, Integer>(); 
   US.put("X", 1200); 
   US.put("Y", 50); 
   US.put("Z", 25552); 
   //can add any number, doesn't have to be three

   Map<String, Integer> UK = new HashMap<String, Integer>(); 
   UK.put("X", 222); 
   UK.put("Y", 52); 
   UK.put("Z", 18);

   hashmap.put("USA", US); 
   hashmap.put("UK", UK);

   //loop through main keys (i.e. US, UK, Africa, etc.)         
   for (String key : hashmap.keySet()) {
       Map<String, Integer> temp = hashmap.get(key); 
        System.out.println("Calculating for " + key);
        Integer sum = 0; 
        //for each key i.e. UK loop through its properties
        for(String otherKey: temp.keySet()) {
            sum = sum + temp.get(otherKey); 
        }
      System.out.println("Sum for " + key + " is " + sum);
   }

输出:

Calculating for USA
Sum for USA is 26802
Calculating for UK
Sum for UK is 292

【讨论】:

  • 我知道我的问题有点不清楚,因为我对整个迭代和计算一无所知。你做得很好,直截了当的回答!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多