java - Understanding HashMap<K,V> -
ok, here bit not understand.
if attempt retrieve object using get() method , null returned, still possible null may stored object associated key supplied get() method. can determine if case passing key of object containskey() method map. returns true if key stored in map
so, how containskey() supposed tell me if value associated key supplied null?
the reference if wanna check. page 553
consider 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 can see put in map 2 values, 1 of null. thene asked map 3 values: 2 of them present (one null), 1 not. @ 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 , third tricky part. key2 present null value so, using get() cannot discriminate whether element not in map or in map null value. but, using containskey() can, returns boolean.
Comments
Post a Comment