如果使用get(),将返回HashMap的哪个键<integer,integer>

时间:2015-11-02 05:51:16

标签: java hashmap

我想知道对于声明如下的HashMap

 static HashMap<Integer, Integer> test= new HashMap<>();

当我们使用test.get(i)时,哪个整数值是关键,哪一个会作为结果返回?

     for(int k=0;k<Array.size();k++)
    {
        test.put(k,0);
    }

    for(int i=0;i<NUM;i++)
{      test.replace(temp.get(i)), occurence.getOrDefault(stringToint.get(featurename), tempp.get(withThis))+1);

          }

3 个答案:

答案 0 :(得分:1)

static HashMap<Integer, Integer> test= new HashMap<>();

Integer i = 3;

test.put(3, 6); //key -> 3, value -> 6

Integer result = test.get(i);

然后,result将为6. 3是密钥,6是与此密钥关联的值。请注意,首先,您必须设置任何键/值对,否则您将获得null

答案 1 :(得分:1)

Class HashMap<K,V>


Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

上面的解释是自我解释的,现在如果你有两个Integer类型,你只需记住参数类型,它将保持与你正在使用的类型相同

Reference

答案 2 :(得分:0)

static HashMap<Integer/*Key*/, Integer/*Value*/> test= new HashMap<>();

get(key)返回与输入键关联的值。如果您的输入是基本类型,在这种情况下为int,它将自动装箱到Integer类,您将获得相应Integer键的值...在您的情况下也是一个Integer。

相关问题