What is difference between HashMap and HashTable?
=========================================================================
HashMap HashTable
=========================================================================
-> No method present inside HashMap is synchronized. -> Every methods inside Hashtable is synchronized.
-> At a time multiple threads are allowed to operate on -> At a time only one thread is allowed to operate on
-> HashMap object simultaneously and hence it is not a -> Hashtable object and hence it is Threadsafe. Threadsafe.
-> Relatively Performance is high. -> Relatively Performance is low.
-> Null is allowed for both keys and values. -> Null is not allowed for both keys and values. Otherwise we will get NullPointerException.
=========================================================================
HashMap LinkedHashMap
=========================================================================
-> Underlying data structure is Hashtable. -> Underlying data structure is combination of Hashtable and LinkedList.
-> Insertion order is not preserved. -> Insertion order is preserved.
-> Introduced in 1.2 version. -> Introduced in 1.4 version.
=========================================================================
IdentityHashMap
=========================================================================
-> It is exactly same as HashMap except the following difference.
-> In case of HashMap, JVM will use .equals(-) method to identify duplicate keys, which is meant for content comparison.
-> But in case of IdentityHashMap, JVM will use == operator to identify duplicate keys which is meant for reference comparison.
WeakHashMap
=========================================================================
-> It is exactly same as HashMap except the following difference.
-> In case of HashMap, if an object associated with HashMap then it is not eligible for garbage collection, eventhough it doesn't contain any external references. i.e., HashMap dominates Garbage Collector.
-> But in case of WeakHashMap, if an object doesn't contain any references then it is always eligible for Garbage Collector eventhough it is associated with WeakHashMap. ie., Garbage Collector dominates WeakHashMap.
How to implement your own HashMap?
*********************************
Hash Map is a implementaion of Map Interface.
HashMap works on the principal of hashing.
-> Map.Entry interface - This interface gives a map entry (key-value pair).
HashMap in Java stores both key and value object, in bucket, as an object of Entry class which implements this nested interface Map.Entry.
-> hashCode() -HashMap provides put(key, value) for storing and get(key) method for retrieving Values from HashMap. When put() method is used to store (Key, Value) pair, HashMap implementation calls hashcode on Key object to calculate a hash that is used to find a bucket where Entry object will be stored. When get() method is used to retrieve value, again key object is used to calculate a hash which is used then to find a bucket where that particular key is stored.
-> equals() - equals() method is used to compare objects for equality.In case of HashMap key object is used for comparison, also using equals() method Map knows how to handle hashing collision (hashing collision means more than one key having the same hash value, thus assigned to the same bucket.In that case objects are stored in a linked list, Where hashCode method helps in finding the bucket where that key is stored,equals method helps in finding the right key as there may be more than one key-value pair stored in a single bucket.
** Bucket term used here is actually an index of array, that array is called table in HashMap implementation. Thus table[0] is referred as bucket0, table[1] as bucket1 and so on.
So it is implementaion of HashMap
How to implement your own ArrayList?
************************************
-> ArrayList is a Resizable-array of an Objects.In Array we don't have the capability of dynamically increasing the size of the array but in ArrayList we can, We know that ArrayList has initial capacity of the element to be added in a list, so first things we need is default initial capacity. second things we need an Array of Objects to store the element of the ArrayList.
->Define default initial capacity of the CustomArrayList.
-private static final int DEFAULT_INITIAL_CAPACITY = 5;
->Now declare an empty CustomArrayList which will return while creating empty CustomArrayList
-private static final Object[] EMPTY_ELEMENT_DATA = {};
->Define the size for the CustomeArrayList
-private int size;
->Now we need to define an Array of an Object to store the elements view sourceprint?
-private transient Object[] customArrayListElementData;
So it is implementaion of ArrayList.
3 Comments
nice
ReplyDeletenice article, waiting for your another :)
ReplyDeleteThank you sharing for this great post.
ReplyDelete