Hashtable - Iterator on keySet, values and entrySet is fail-fast in java

Hashtable - Iterator on keySet, values and  entrySet is  fail-safe or fail-fast?

The iterators returned by the iterator() method of the collections returned by all three Map's “collection view methods" are fail-fast. Means any structural modification made to Hashtable like adding or removing elements during Iteration will throw java.util.ConcurrentModificationException.

HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences

Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class HashtableTest {
public static void main(String args[]){
   Map<Integer,String> hashtable=new Hashtable<Integer,String>();
   hashtable.put(11, "ankit");
   hashtable.put(21, "javaMadeSoEasy");
   System.out.println("\n---1. Iterate on keys, by obtaining iterator on keySet---");
   Iterator<Integer> keyIterator=hashtable.keySet().iterator();
          hashtable.put(4,"newEle1");//unComment to avoid ConcurrentModificationException
   System.out.println("\n---2. Iterate on values, by obtaining iterator on values---");
   Iterator<String> valueIterator=hashtable.values().iterator();
          hashtable.put(4,"newEle1");//unComment to avoid ConcurrentModificationException
   System.out.println("\n---3. Iterate on entry, by obtaining iterator on entrySet---");
   Iterator<Entry<Integer, String>> entryIterator=hashtable.entrySet().iterator();  
          hashtable.put(4,"newEle1");//unComment to avoid ConcurrentModificationException
---1. Iterate on keys, by obtaining iterator on keySet---
Exception in thread "main" java.util.ConcurrentModificationException
   at java.util.Hashtable$Enumerator.next(Unknown Source)
   at hashtable_iterator_fail.HashtableTest.main(HashtableTest.java:24)


HashMap - Iterate on keys by obtaining keySet, Iterate on values by obtaining values, Iterate on entry by obtaining entrySet

HashMap - synchronizing map using Collections.synchronizedMap

ArrayList - add, add element at specific index methods program

Must read for you :