Reputation: 46
Should be simple enough but it's driving me nuts!
Have a HashMap with a Key:Value pair as Long:Dto respectively.
When I debug, I can see the one entry in the Hashmap with a Long value as the key - but when I try a get() using the Key, I'm getting a null back.
For the following:
Map<Long, Dto> response = getMap();
System.out.println("Contents of HashMap: " + response);
System.out.println("Size of HashMap: " + response.size());
System.out.println("HashMap Keyset: " + response.keySet());
System.out.println("HashMap Contains: " + response.containsKey(19999984L));
System.out.println("HashMap Object: " + response.get(19999984L));
I'm getting the following output:
Contents of HashMap: {19999984={productOptionId=19999984, amount={amount=20.99, currency=EUR}, pricingType=MSRP}}
Size of HashMap: 1
HashMap Keyset: [19999984]
HashMap Contains: false
HashMap Object: null
Any suggestions would be appreciated here... this should work surely!
Maybe I'm missing something obvious.
Answer: So it seems my key value is of String type - as below:
System.out.println("Contents of HashMap: " + response);
System.out.println("Size of HashMap: " + response.size());
System.out.println("HashMap Keyset: " + response.keySet());
System.out.println("HashMap Contains: " + response.containsKey("19999984"));
System.out.println("HashMap Object: " + response.get("19999984"));
System.out.println("HashMap Class: " + response.getClass());
Upvotes: 0
Views: 453
Reputation: 342
Your key in the hashmap is a int type and your contains method has 19999984L which is long type.
Contents of HashMap: {19999984={productOptionId=19999984, amount={amount=20.99, currency=EUR}, pricingType=MSRP}}
Size of HashMap: 1
System.out.println("HashMap Contains: " + response.containsKey(19999984L));
That's the reason for false.
Upvotes: 3