Posts

Showing posts from October, 2015

WeakHashMap

              WeakHashMap is one of the lesser known member of Map family. It is a special kind of hashtable based Map implementation which stores weak references to the keys. That means keys of this kind of data structure will be stored in a WeakReference . So Entry of the Map will be reclaimed by the garbage collector when its key object is no longer used. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed.              In HashMap , if reference of a key object no longer exist , corresponding Entry ( key – value pair ) will not be removed from Map . Garbage collector can not collect live objects . As long as Map object is live , all buckets or Entry are live . So they can not be collected by garbage collector .             WeakHashMap us...

IdentityHashMap

           IdentityHashMap is one of the important member of Map family. It had been introduced in jdk 1.4 . But this data structure could not get that much of popularity as that of HashMap . This Hashtable based data structure also implements Map , Serializable , Cloneable and extends AbstractMap class. As per Oracle documentation " This class is  not  a general-purpose  Map  implementation! While this class implements the  Map  interface, it intentionally violates  Map's  general contract, which mandates the use of the  equals  method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required. "           To be precise this is a special implementation of Map interface which uses reference-equality instead of well known logical-equality to compare keys , i.e. , it does not use equal() and hashCode() ...

HashMap internal working principle

Image
           HashMap data structure works on the principle of hashing. Hashing principle works on any pre defined algorithm maintaining certain criteria to assign a consistent value or code to an object . To be precise hashing algorithm must be such that it should always provide same hash code for same or equal objects. There is a strong relation between hashcode and equal method . That we will see later in this article when HashMap will be explained in depth. Hash code is being defined by the hashCode() method of Object class which is the mother class of java. What is hashCode() :          HashCode() is a method which belongs to Object class.It calculates an integer value for every object. Definition of hashCode() in Object class is : 1 public native int hashCode();         This method is defined as native because it deals with native data . It assigns an integer value whi...

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 genera...