Thursday, 9 July 2015

Java : Collection Framework : HashMap/Hashtable (fail-fast)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=33wMCTcvm_g&list=UUhwKlOVR041tngjerWxVccw

HashMapFailFastExample.java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class HashMapFailFastExample
{

    public static void main( String[] args )
    {

        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

        hashMap.put(1, "Apple");
        hashMap.put(2, "Ball");
        hashMap.put(3, "Cat");

        System.out.println("hashMap : " + hashMap);

        Set<Integer> keys = hashMap.keySet();

        /*
         * Iterator in the HashMap/Hashtable is fail-fast
         */
        Iterator<Integer> iterator = keys.iterator();

        while( iterator.hasNext() )
        {
            Integer key = iterator.next();

            /*
             * Throw ConcurrentModificationException if we modifies the map
             * structurally by adding or removing any element except Iterator's
             * own remove() method.
             */

            hashMap.put(4,"Dove");          
            System.out.println("key : " + key);

        }

    }
}
Output
hashMap : {1=Apple, 2=Ball, 3=Cat}
key : 1
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
    at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
    at HashMapFailFastExample.main(HashMapFailFastExample.java:28)

HashTableFailFastExample.java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class HashMapFailFastExample
{

    public static void main( String[] args )
    {

        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

        hashMap.put(1, "Apple");
        hashMap.put(2, "Ball");
        hashMap.put(3, "Cat");

        System.out.println("hashMap : " + hashMap);

        Set<Integer> keys = hashMap.keySet();

        /*
         * Iterator in the HashMap/Hashtable is fail-fast
         */
        Iterator<Integer> iterator = keys.iterator();

        while( iterator.hasNext() )
        {
            Integer key = iterator.next();

            /*
             * Throw ConcurrentModificationException if we modifies the map
             * structurally by adding or removing any element except Iterator's
             * own remove() method.
             */

            hashMap.put(4,"Dove");          
            System.out.println("key : " + key);

        }

    }
}

Output
hashtable : {3=Cat, 2=Ball, 1=Apple}
3
2
1
hashtable : {4=Dove, 3=Cat, 2=Ball, 1=Apple}

To Download CollectionFailFastDemoApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionFailFastDemoApp.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge