Hashmap contains key return false even when key exists

1.4k views Asked by At

I have made a hashmap Map percentages= new HashMap();, I have entered values and keys into it and that was successful, but when I try to get the or try ContainsKey method then it returns false.

Please see the attached screen shot:enter image description here

2

There are 2 answers

0
Ezhilan Mahalingam On BEST ANSWER

When assigning values to Map, you may used different datatype and when checking for key, you are using integer, it won't work.

Map a = new HashMap();
    a.put("1", 12);
    a.put("2", 32);

    System.out.println(a.containsKey(1));

This will always return false.

2
Juned Ahsan On

you are calling contains on new ArrayList object and not on Map