Search code examples
javaandroiddata-structureshashmapcontainskey

Hashmap contains key return false even when key exists


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


Solution

  • 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.