-3

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

Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53
  • 1
    would you like to show your code ? – Rookie007 Jun 25 '15 at 02:20
  • would you please show you code? – JaskeyLam Jun 25 '15 at 02:27
  • 2
    Without code, we can't tell what you did wrong. For all we know, maybe you have a string of `42` as a key in the `HashMap`, and are trying to do a `ContainsKey()` with an integer `42`. That's just one way you could have gotten it wrong. And if your post is to be believed, doesn't look like you are using a typed `HashMap`, so it is quite possible. Or maybe you are experiencing a subtle bug related to auto boxing. Who knows? – sstan Jun 25 '15 at 02:31

2 Answers2

1

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

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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.

Ezhilan Mahalingam
  • 2,838
  • 3
  • 16
  • 16