Q-1) WHAT ARE THE DIFFERENCES BETWEEN HASHMAP AND CONCURRENTHASHMAP? ==================================================================== 1) ConcurrentHashMap is a thread-safe object. It means, multiple threads can concurrently use to read/write elements without causing inconsistency. But HashMap is not a thread-safe object. It means, if multiple threads are concurrently writing elements this leads an inconsistency. 2) In ConcurrentHashMap, no null as key and value. If we can use null as key or value, we can get an exception "NullPointerException". HashMap can allow one null as key and more than one null as value. 3) ConcurrentHashMap provide better performance with multi-thread applications. Whereas the HashMap provide better performance with single thread applications. 4) ConcurrentHashMap iteration is ==> Fail-safe HashMap iteration is ==> Fail-fast ================================================ Fail-safe & Fail-Fast ===================== -> The iterations on the collections can be categorized into two types: 1) Fail-safe Iteration 2) Fail-Fast Iteration -> These iterations can use to describe the behavior of iterators, that how the iterators behave because of modification (adding/removing of element only) on the collection during the iteration. -> Here, because of the modification during the iteration, the structure of the collection can be get changed during the iteration. -> If the iterator can return an exception (ConcurrentModificationException) because of this modification on the collection during the iteration, that iterator is called "Fail-Fast iterator". -> Fail-Fast Iterator can use on: ArrayList, LinkedList, HashMap, TreeSet etc. package p1; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class MainClass { public static void main(String[] args) { List l = new ArrayList<>(); l.add("ABC"); l.add("DEF"); l.add("GHI"); Iterator i = l.iterator(); while(i.hasNext()) { System.out.println(i.next()); // l.add("JKL"); l.remove("ABC"); } } } Fail-Safe Iterator: =================== -> The Fail-safe iterator can allow to define on the copy of the collection. -> The Fail-safe iterator never throw an exception(ConcurrentModificationException) while the modification on the given collection during the iteration. -> The Fail-safe iterator can implement on: "ConcurrentHashMap" object. package p1; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; public class Main { public static void main(String[] args) { ConcurrentHashMap map = new ConcurrentHashMap<>(); map.put("a", "apple"); map.put("b", "banana"); map.put("c", "cherry"); Iterator itr = map.keySet().iterator(); while(itr.hasNext()) { String s = itr.next(); System.out.println(s); map.put("d","doll"); } } }