Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding HashMap<K,V>

Tags:

java

hashmap

Ok, here is the bit I do not understand.
If you attempt to retrieve an object using the get() method and null is returned, it is still possible that null may be stored as the object associated with the key you supplied to the get() method. You can determine if this is the case by passing your key of the object to containsKey() method for map. This returns true if key is stored in the map
So, how is containsKey() supposed to tell me if the value associated with the key supplied is null?
This is the reference if you wanna check. Page 553

like image 404
A User Avatar asked Jun 04 '12 19:06

A User


People also ask

What is K and V in HashMap?

Type Parameters: K - the type of keys maintained by this map V - the type of mapped values All Implemented Interfaces: Serializable, Cloneable, Map<K,V> Direct Known Subclasses: LinkedHashMap, PrinterStateReasons.

What is HashMap and how it works?

HashMap stores entries into multiple singly linked lists, called buckets or bins. Default number of bins is 16 and it's always power of 2. HashMap uses hashCode() and equals() methods on keys for get and put operations. So HashMap key object should provide good implementation of these methods.

What is values () in HashMap in Java?

values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap.

What is a good load factor for HashMap?

As a general rule, the default load factor (. 75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put).


4 Answers

Map<String, Object> map = new HashMap<String, Object>();
map.put("Foo", null);
System.out.println(map.containsKey("Foo"));
System.out.println(map.containsKey("Boo"));

OUTPUT:

true
false

get() returns null in two cases:

  • The key does not exist in the map.
  • The key does exist but the associated value is null.

You can't tell from get() which is true. However, containsKey() will tell you if the key was present in the map, regardless of whether its associated value was null.

like image 99
Eng.Fouad Avatar answered Oct 23 '22 02:10

Eng.Fouad


Consider this simple snippet of code:

Map<String, String> m = new HashMap<String, String>();
m.put("key1", "value1");
m.put("key2", null);

System.out.println("m.get(\"key1\")=" + m.get("key1"));
System.out.println("m.containsKey(\"key1\")=" + m.containsKey("key1"));

System.out.println("m.get(\"key2\")=" + m.get("key2"));
System.out.println("m.containsKey(\"key2\")=" + m.containsKey("key2"));

System.out.println("m.get(\"key3\")=" + m.get("key3"));
System.out.println("m.containsKey(\"key3\")=" + m.containsKey("key3"));

As you can see I put in the map two values, one of which is null. Thene i asked the map for three values: two of them are present (one is null), one is not. Look at the result:

m.get("key1")=value1
m.containsKey("key1")=true
m.get("key2")=null
m.containsKey("key2")=true
m.get("key3")=null
m.containsKey("key3")=false

The second and the third are the tricky part. key2 is present with null value so, using get() you cannot discriminate whether the element is not in the map or is in the map with a null value. But, using containsKey() you can, as it returns a boolean.

like image 25
loscuropresagio Avatar answered Oct 23 '22 03:10

loscuropresagio


(get() == null && containsKey()) == value is null
like image 3
Todd Gibson Avatar answered Oct 23 '22 04:10

Todd Gibson


containsKey would tell you if the key is in the hashmap at all. Consider the case where a key is present with null value and the other case in which the key which you are looking for is not at all in the hashmap.

like image 1
FSP Avatar answered Oct 23 '22 04:10

FSP