Java Hashmap
A collection allows a group of objects to be treated as a single
unit. Map is one of the core interfaces of java collection framework
that defines operations for maintaining mappings of keys to values.
Map interface does not implement Collection interface, because
it does not contain elements but contains entries of keys and their
corresponding values (i.e. called mapping).
Map does not allow duplicate keys. So there is utmost one value
that is mapped with the given key. Both key and value must be an
Object (Primitive values must be wrapped).
HashMaps will automatically grow when you add too many elements.
However, growing requires copying, rehashing and rechaining, which
affects its overall performance.
Performance of HashMap depends on two important factors that are
• Initial Capacity and
• Load Factor
Initial Capacity is the capacity at the time the HashMap is created.
Load factor determines when to increase the capacity of the HashMap.
The default load factor is 0.75.
Important Note: The initial capacity is not the actual number of
elements you plan to store in HashMap. Say for example, if you set
initial capacity of 100 and the load factor is 0.75, then the capacity
of HashMap will be automatically increased when it reaches to 75
not 100.
Note : As opposed to Hashtable, HashMap class permits null values
and null keys. In addition to this HashMap is also not synchronized
as Hashtable. If needed it should be synchronized externally using
Map myMap = Collections.synchronizedMap (new HashMap ());
This implementation of HashMap provides constant-time performance
for the basic operations like get and put.
|