HashMap



          HashMap class stores key and value pairs. It is hash table based implementation of Map Interface. Though java provides Map interface along with Collection framework , Map is not considered as true Collection due to few of features of Map . HashMap is one of the member of Map family.It can not contain duplicate values but it allows null key as well as value. HashMap implements Map , Serializable and Cloneable interface and extends AbstractMap concrete class.

1
2
3
4
public class HashMap<K, V> extends AbstractMap<K, V> 
implements Map<K, V>, Cloneable, Serializable {
  .....
}

Significance of class definition :

  • HashMap implements Map interface as it is member of Map family which provides abstraction for generalised functionality of Map.
  • AbstractMap class provides skeleton of the Map implementation to reduce general implementation effort of Map. It provides definition of most of the generalised methods of Map. For unmodifiable Map programmers need to extends only AbstractMap and provide entrySet() method .
    public abstract Set<Map.Entry<K, V>> entrySet();
    For modifiable Map programmer has to override put() and get() .

  • Serializable is a marker Interface . HashMap implements this interface to support serialization.
  • HashMap implements Cloneable marker interface to support cloning.
  • In java 1.5 java introduced Generics . HashMap is also a generic class where K represents key and V represents value object.

          HashMap implementation is based on Hashtable but there are differences between HashMap and HashTable. Main difference is that HashTable methods are thread safe but HashMap methods are not thread safe , i.e . , methods of Hashtable class implement synchronized keyword. Another important difference is that Hashtable does not support null key and value where as HashMap supports one null key and multiple null values.

Properties of HashMap :

  • HashMap contains value object based on key object as per hashCode() method implemented by that Object.
  • HashMap contains unique key . If programmer tries to put existing key Object again then it will over write previous value and return old value.
  • HashMap allow only one null key as it maintains unique key concept and multiple null value.
  • HashMap is not synchronized or thread safe.
  • HashMap does not maintain any order as it stores key and value using hashing concept.

HashMap Methods :

  • HashMap provides four constructors :
            public  HashMap()
    public HashMap(int paramInt)
    public HashMap(int paramIntfloat paramFloat)
    public HashMap(Map<? extends K, ? extends V> paramMap)

  • public V put(K paramK, V paramV) : Add key value pair to the Map ( EntrySet or Bucket).
  • public V get(Object paramObject) : Retrieve particular value Object corresponding to particular key Object.
  • public boolean isEmpty() : Check whether Map is empty or not. If there is no key value pair stored in Map , it returns true.
  • public int size() : It returns no of key value par present in Map.
  • public boolean containsKey(Object paramObject) : Returns true only if intended key object is present in Map.
  • public boolean containsValue(Object paramObject) : Returns true only if intended value object is present in Map.
  • public void putAll(Map<? extends K, ? extends V> paramMap) : Add all values of a Map .
  • public V remove(Object paramObject) : Accept key object and remove particular key value pair and return corresponding value.
  • public void clear() : Remove all key value pair.
  • public Object clone() : Create a copy of HashMap containing all key value pair and return the same as clone of the HashMap.
  • public Set<K> keySet() : Returns a set of all key objects present n the HashMap.
  • public Collection<V> values() : Returns a collection of all values present in HashMap.

Example of HashMap :

public static void main(String[] args)
{
HashMap<String, String> hashMap = new HashMap<String, String>();
//to find out if hashMap is empty
System.out.println(hashMap.isEmpty());
//to find out size of the HashMap
int size = hashMap.size();
System.out.println("Size of the HashMap : "+size);
//adding data to the hashMap
hashMap.put("India""Delhi");
hashMap.put("Japan""Tokyo");
hashMap.put("UK""London");
hashMap.put("USA""Washington");
String key=null;
//to iterate the Map
System.out.println("Contents of HashMap : ");
Iterator<String> itr = hashMap.keySet().iterator();
while(itr.hasNext())
{
key = itr.next();
System.out.println("Key : "+key+" Value : "+hashMap.get(key));
}
//to check if key exists in HashMap
System.out.println("India exists : "+hashMap.containsKey("India"));
System.out.println("Delhi exists : "+hashMap.containsValue("Delhi"));
//over writing old value
hashMap.put("India""Kolkata");
//iterate modified HashMap
System.out.println("Contents of HashMap after modification : ");
itr = hashMap.keySet().iterator();
while(itr.hasNext())
{
key = itr.next();
System.out.println("Key : "+key+" Value : "+hashMap.get(key));
}
//to remove element from HashMap
System.out.println("Element removed : "+hashMap.remove("UK"));
System.out.println("Contents of HashMap after removal : ");
itr = hashMap.keySet().iterator();
while(itr.hasNext())
{
key = itr.next();
System.out.println("Key : "+key+" Value : "+hashMap.get(key));
}
//clear HashMap
hashMap.clear();
System.out.println("Size of the HashMap : "+hashMap.size());
}


Output :

If hashMap is empty : true
Size of the HashMap : 0
Contents of HashMap :
Key : USA Value : Washington
Key : UK Value : London
Key : Japan Value : Tokyo
Key : India Value : Delhi
India exists : true
Delhi exists : true
Contents of HashMap after modification :
Key : USA Value : Washington
Key : UK Value : London
Key : Japan Value : Tokyo
Key : India Value : Kolkata
Element removed : London
Contents of HashMap after removal :
Key : USA Value : Washington
Key : Japan Value : Tokyo
Key : India Value : Kolkata
Size of the HashMap : 0


Comments

Popular posts from this blog

What is Stream API in Java

HashMap internal working principle