Java Map Interface
A map contains values on the basis of key, i.e. key and value pair. Each key and value
pair is known as an entry. A Map contains unique keys.
A Map is useful if you have to search, update or delete elements on the basis of a key.
Java Map Hierarchy
There are two interfaces for implementing Map in java: Map and SortedMap, and three
classes: HashMap, LinkedHashMap, and TreeMap. The hierarchy of Java Map is given
below:
A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and
LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or
value.
A Map can't be traversed, so you need to convert it into Set
using keySet() or entrySet() method.
Class Description
HashMap HashMap is the implementation of Map, but it doesn't maintain any order.
LinkedHashMap LinkedHashMap is the implementation of Map. It inherits HashMap class. It
maintains insertion order.
TreeMap TreeMap is the implementation of Map and SortedMap. It maintains
ascending order.
Hashmap
Program-1
Hashtable
Program-
HashMap and Hashtable
HashMap and Hashtable both are used to store data in key and value form. Both are
using hashing technique to store unique keys.
But there are many differences between HashMap and Hashtable classes that are given
below.
HashMap Hashtable
1) HashMap is non synchronized. It is not- Hashtable is synchronized. It is thread-
thread safe and can't be shared between many safe and can be shared with many
threads without proper synchronization code. threads.
2) HashMap allows one null key and Hashtable doesn't allow any null key or
multiple null values. value.
3) HashMap is a new class introduced in Hashtable is a legacy class.
JDK 1.2.
4) HashMap is fast. Hashtable is slow.
5) We can make the HashMap as synchronized Hashtable is internally synchronized and
by calling this code can't be unsynchronized.
Map m =
Collections.synchronizedMap(hashMap);
6) HashMap is traversed by Iterator. Hashtable is traversed by Enumerator
and Iterator.
7) Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.
8) HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.
HashMap vs Hashtable
1) HashMap is non-synchronized. This means if it’s used in multithread
environment then more than one thread can access and process the HashMap
simultaneously.
Hashtable is synchronized. It ensures that no more than one thread can access
the Hashtable at a given moment of time. The thread which works on Hashtable
acquires a lock on it to make the other threads wait till its work gets completed.
2) HashMap allows one null key and any number of null values.
Hashtable doesn’t allow null keys and null values.
3) HashMap implementation LinkedHashMap maintains the insertion order
and TreeMap sorts the mappings based on the ascending order of keys.
Hashtable doesn’t guarantee any kind of order. It doesn’t maintain the mappings
in any particular order.
4) Initially Hashtable was not the part of collection framework it has been made a
collection framework member later after being retrofitted to implement the Map
interface.
HashMap implements Map interface and is a part of collection framework since
the beginning.
5) Another difference between these classes is that the Iterator of the HashMap
is a fail-fast and it throws ConcurrentModificationException if any other Thread
modifies the map structurally by adding or removing any element except iterator’s
own remove() method. In Simple words fail-fast means: When calling
iterator.next(), if any modification has been made between the moment the
iterator was created and the moment next() is called, a
ConcurrentModificationException is immediately thrown.