Why use hashMap when ConcurrentHashMap is there… | Techartifact

ConcurrentHashMap is sort of hidden class. Not many people know about it and not many people care to use it. The class offers a very robust and fast (comparatively, we all know java concurrency isn’t the fastest) method of synchronizing a Map collection.

On the internet there is lot of article which tell difference between hashmap and ConcurrentHashMap. This class obeys the same functional specification as Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully inter-operable with Hashtable in programs that rely on its thread safety but not on its synchronization details.
Ideally we should not ask for difference.Its like comparing orange with apple.One is giving you synchronize feature and other one is note.

concurrentHashMap is thread-safe implementation of Map interface. In this class put and remove method are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance.

If you use HashMap in your application, it is working perfectly in development or test environment ,but gave pain in production..
But obvious is—when heavy load, HashMap behaves starting weird. If we use HashTable.Hashtable’s offer concurrent access to their entries, with a small caveat, the entire map is locked to perform any sort of operation. While this overhead is ignorable in a web application under normal load, under heavy load it can lead to delayed response times and overtaxing of your server for no good reason.
This is where ConcurrentHashMap’s step in. They offer all the features of Hashtable with a performance almost as good as a HashMap. ConcurrentHashMap’s accomplish this by a very simple mechanism.Instead of Map’s lock ,the collection keep a list of 16 lock by default,each of which is used to guard (or lock on) a single bucket of the map. This effectively means that 16 threads can modify the collection at a single time (as long as they’re all working on different buckets). Infact there is no operation performed by this collection that locks the entire map. The concurrency level of the collection, the number of threads that can modify it at the same time without blocking, can be increased. However a higher number means more overhead of maintaining this list of locks.

Retrieval operations on a ConcurrentHashMap do not block unless the entry is not found in the bucket or if the value of the entry is null. In such a case the map synchronizes on the bucket and then tries to look for the entry again just in case the entry was put or removed right after the get in synchronized mode.Removal operations do require a bit of overhead. All removal operations require the chain of elements before and after to be cloned and joined without the removed element. Since the value of the map key is volatile (not really, the value of the inner Entry class is volatile) if a thread already traversing the bucket from which a value is removed reaches the removed element, it automatically sees a null value and knows to ignore such a value.

HashMap vs Hashtable

HashMap vs Hashtable

Hashtable and HashMap are both key-value based data structure. Both of them allows the access data based on key.
Both of them some differences in storing values and performance over iteration.

Some of the basic differences are following

HashMap HashTable
Synchronized Un-synchronized  Synchronized
Allow null Allowed Null for key and Value Not allowed, Null pointer would be thrown for Null
 Support in JDK since 1.2  1.0
 Subclass of Dictionary AbstractMap
 load factor  .75 .75

Hashtable

Hashtable performance is effected by its initial capacity and load factor provided at creation of object. In case of high load factor it grows it self.
At the time of creation there should not be too much initial capacity other-wise it would wastage of space. Higher the value of load factor will save to space, but lower the load factor will increase the time for searching entry.

Hashmap

Hashmap provide no guarantees to the order of the map. Hashmap is not synchronized, It means that multiple thread can access the same instance at same time, which can lead to
structal modification.

HashMap is better for non-threaded applications, as unsynchronized Objects perform better than synchronized ones.

HASHMAP IN JAVA

A map is an interface in the java.util package which stores the association between key and its corresponding value. A map cannot contain duplicate keys; each key can map to at most one value Map differs from array in a way that we can store a value at a particular index in arrays but a Map determines the index itself and does this based on the value on the key.
HASHMAP is a class which implements the map interface.
Hash map is a data structure which uses a hash function to map identifying values, known as keys (e.g., a person’s name) to their associated values (e.g., their telephone number).
The hash function transforms the key into the index (the hash) of an array element where the corresponding value is to be sought.
Take for instance the case of a phonebook. you can have a map where the keys are phone numbers and the value is the name of the person..
Given a key you can find its value.
Ex.[“key”, “value”]=[“88028”,”vinay”]
With the given key value i.e 88028 one can find its value….. which is vinay.
Take another example…..

import java.util.Map; 
import java.util.HashMap; 
 
public class Map_example { 
Map<Integer, String> addPhoneNum = new HashMap<Integer, String>(); 
public void addPhoneNum(Integer phonenumber, String name) { 
addPhoneNum.put(phonenumber, name); 
} 
public String getName(Integer phonenumber) { 
return addPhoneNum.get(phonenumber); 
} 
 public static void main(String[] args) { 
Map_example m1 = new Map_example(); 
m1.addPhoneNum(9911538992 "vinay"); 
 
System.out.println( 
m1.getName(9911538992)); 
 
} 

Main features of HASHMAP are.
• HashMap is a library class in Java.
• It is already implemented, hence can be used immediately HashMap is a library class in Java.
• HashMap stores only object references. That’s why, it’s impossible to use primitive data types like double or int. Use wrapper class (like Integer or Double) instead.
• For multi-theaded(synchronized) array class use Hashtable (java.lang.Hashtable)

The two most important HashMap’s methods are:
• get( Object key ) –this method returns the value associated with specified key in this hash map, or null if there is no value for this key
• put(K key, V value) – this method associates the specified value with the specified key in the map.

Internally a HashMap maintains an array. To have the HashMap work efficiently, the array must be large enough so that the key/value pairs are well-distributed and the performance is not affected. Therefore, the HashMap maintains two (customizable) variables :
1. Capacity
2. LoadFactor.
The capacity is the length of the internal array whereas the LoadFactor controls when the capacity should be increased.