HashMap is a Hash table based implementation of the Map interface. For more details on hashing, you can refer to http://javajee.com/hashing-basics.
Provides all of the optional map operations
Permits null values and the null key.
Roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
Makes no guarantees regarding order of the map
Provides constant-time performance for the basic operations (get and put)
assuming the hash function disperses the elements properly among the buckets.
An instance of HashMap has two parameters that affect its performance:
initial capacity and load factor.
The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
Chosing right values for capacity and load factor are important:
The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations.
If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.
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).
In general, don't set the initial capacity too high (or the load factor too low).
The default load factor (.75) offers a good tradeoff between time and space costs.
is not synchronized:
If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally
Could synchronize on some object that naturally encapsulates the map.
Else, map could be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
The iterators returned by all collection view methods" are fail-fast:
fails quickly and cleanly, rather than risking undetermined behaviour in the future.
if the list is structurally modified at any time after the iterator is created, except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
Fail-fast iterators throw ConcurrentModificationException on a best-effort basis and cannot be guaranted; so we should not depend on it while coding.
HashMap provides all of the optional map operations.
Additional important methods include clone(), which returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.