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 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.
Comments
Post a Comment