lonesome
lonesome

Reputation: 2552

which key of a HashMap<Integer,Integer> would be returned if using get()

I was wondering that for a HashMap that is declared as below:

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

when we use test.get(i) which integer value is the key and which one would be returned as the result?

     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);

          }

Upvotes: 0

Views: 636

Answers (3)

H&#233;ctor
H&#233;ctor

Reputation: 26044

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

Integer i = 3;

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

Integer result = test.get(i);

Then, result will be 6. 3 is the key and 6 is the value associated to this key. Note that first, you have to set any key/value pair, or you will get null.

Upvotes: 1

SSH
SSH

Reputation: 1627

Class HashMap<K,V>


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

The above explanation is self explanatory , now if you have both Integer types you just keep in mind the parameters types,it will remain same what ever the types you are taking

Reference

Upvotes: 1

Pavan Dittakavi
Pavan Dittakavi

Reputation: 3181

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

get( key ) returns you the value associated with the input key. If your input is a primitive type, int in this case, it gets auto boxed to Integer class and you would get the value for the corresponding Integer key...which is also an Integer in your case.

Upvotes: 0

Related Questions