HashMap Tutorial : HashMap belongs to the collections framework being derived from Map interface. It is just similar to Hashtable of legacy classes (JDK 1.0 data structures are known as legacy classes and the other are Vector, Properties and Stack). The differences between are narrated.
- Duplicate keys: Both will not accept duplicate keys; if added earlier is overridden.
- Synchronization: Hashtable methods are synchronized implicitly and best suitable in multithreaded environment where as HashMap are not. But synchronized version of HashMap can be obtained.
- Null keys and values: Hashtable does not permit null keys and null values (throws NullPointerException), but HashMap permits both (illustrated in HashMap General).
- Order of Elements: Order of elements retrieved may differ from execution to execution in both.
- Default Capacity: Default capacity of Hashtable is 11 and HashMap is 16. The load factor is same of 0.75 for both.
The following is the class signature of HashMap class
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable
Following are the constructors
- HashMap(): Creates HashMap object with an initial capacity of 16 elements and when exhausted increments by 0.75, known as load factor.
- HashMap(int startCap): A HashMap object is created with an initial capacity of startCap and when the capacity is exhausted, increments by 0.75.
- HashMap(int startCap, float lfact): A HashMap object is created with an initial capacity of startCap and when the capacity is exhausted, increments by lfact, load factor. Negative values raise IllegalArgumentException.
- HashMap(Map m1): A HashMap object is created with default elements of map m1. Map m1 elements are added to HashMap. Like this, HashMap and Map are interoperable. The initial capacity is just sufficient to store the elements of Map m1 and increments by the default load factor 0.75. If m1 does not contain elements, NullPointerException is thrown.
HashMap Tutorial: HashMap Internal Mechanism
HashMap includes two properties – capacity and load factor. Capacity is the storing capacity of the hash Map and load factor is the incrementing factor used to increase the existing capacity when it is exhausted. The default capacity is 16 (like vector comes with 10) and default load factor is 0.75. The initial capacity and load factor can be customized by using above second and third constructors. When the current capacity is used out, the increment will be on the existing capacity like existingCapacity * 0.75. Like vector, a new location is searched for the new capacity and copies the elements of the old location into location. This is called as rehashing as rehash() method is called. Rehashing decreases the performance very considerably. To overcome this, the second overloaded constructor can be used where initial capacity required by the programmer can be given at the time of object creation itself.
It inherits all the methods from Map and adds following method.
- Object clone(): Returns a copy of the HashMap. The cloned object contains the same key/value pairs.
HashMap TutoriaHashMap Tutorial: Synchronized HashMap
We know earlier, the HashMap methods are not synchronized. A synchronized version of HashMap can be obtained as follows.
HashMap hm1 = new HashMap();
Map m1 = Collections.synchronizedMap(hm1);
Now, m1 can be used in a multithreaded environment for thread-safe operations. But still hm1 is not synchronized and can be used in regular programming.
Generally, on the HashMap object we create, all the operations of addition and deletion can be done. But sometimes, it may be required read-only HashMap object and can be obtained as follows.
HashMap hm1 = new HashMap();
Map m1 = Collections.unmodifiableMap(hm1);
Now on m1, the operations like addition and deletion etc. cannot be done; only retrieval and iteration of elements can be done.
Why it is not feasible to use the hash map in the multithreading environment?
Whenever there are multiple threads, synchronization come into picture. As HashMap is not synchronized, so you can’t use it in multiple threading environment until you synchronized the same.
Best alternative to use HashMap in multiple threading environment is “ConcurrentHashMap” because all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access which is the case in HashMap.
Three programs are given on HashMap Tutorial with different functionalities used very often in coding.
- HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done.
- HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning are shown.
- HashMap Sort: Sorting operations on keys and values are done. Special attention is paid to retrieve the key when a value is given.