Sunday, August 7, 2016

HashMap Quick Reference

Permits multiple null values and one null key
Equivalent to Hashtable, except that it is unsynchronized( i.e. it’s methods are unsynchronized) and permits nulls (Hashtable does not allows null keys or values)
Does not guarantee that the order of map will remain constant over time.
Provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.
Iteration time is proportional to the "capacity" of the HashMap instance plus its actual size
In multi-threaded environment, to prevent accidental unsynchronized access to the map that can modify HashMap structurally, wrap HashMap using Collections.synchronizedMap
Map m = Collections.synchronizedMap(new HashMap(...));
Iterators of HashMaps are fail-fast
Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. But one must not write program that depends on this behaviour.
get() method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. However, null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null.

When duplicate key put into HashMap, put() method overrides old value with new value and returns the old value. put() method returns null if this map contains no mapping for the key. However, a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null.

No comments:

Total Pageviews