Showing posts with label Java Collection Framework. Show all posts
Showing posts with label Java Collection Framework. Show all posts

Friday, 10 July 2015

Java : Collection Framework : Collections (emptyList)


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

Click the below Image to Enlarge
Java : Collection Framework : Collections (emptyList)
Java : Collection Framework : Collections (emptyList)
Java : Collection Framework : Collections (emptyList)
CollectionsExample.java

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 Example of:  

 Method:

 emptyList() 

 Returns:

 an empty immutable list.
------------------------------
 Method:

 emptySet()

 Returns:

 the empty set.
-----------------------------
 Method:

 emptyMap()

 Returns:

 the empty map

 */

public class CollectionsExample
{

    public static void main(String[] args)
    {

        /*
         * Returns an empty list (immutable).
         */
        List<String> emptyList = Collections.emptyList();
        System.out.println("emptyList : " + emptyList);

        /*
         * Returns an empty set (immutable).
         */
        Set<String> emptySet = Collections.emptySet();
        System.out.println("emptySet : " + emptySet);

        /*
         * Returns an empty map (immutable).
         */
        Map<String, String> emptyMap = Collections.emptyMap();
        System.out.println("emptyMap : " + emptyMap);

    }

}
Output
emptyList : []
emptySet : []
emptyMap : {}
To Download CollectionsDemoEmptyList Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoEmptyList.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
  • Java : Collection Framework : Collections (emptyList Country)


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

    CountryInfo.java
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CountryInfo
    {
    
        List<String> countryList = new ArrayList<String>()
        {
            private static final long serialVersionUID = 1L;
    
            {
                add("India");
                add("Pakistan");
                add("China");
                add("Iran");
            }
        };
    
        public List<String> getCountryList(String startingWith)
        {
    
            if (startingWith == null)
            {
                /*
                 * You should always return an emptyList instead of null
                 */
                //return null;
                return Collections.emptyList();
            }
    
            ArrayList<String> filteredCountryList = new ArrayList<String>();
            for (String countryName : countryList)
            {
                if (countryName.startsWith(startingWith))
                {
                    filteredCountryList.add(countryName);
                }
            }
            return filteredCountryList;
        }
    
    }
    
    Client.java
    import java.util.List;
    
    /*
     Method: 
    
     public static final <T> List<T> emptyList()
    
     Parameters:
    
     c - the collection for which an enumeration is to be returned.
    
     Returns:
    
     an empty immutable list.
    
     */
    
    public class Client
    {
    
        public static void main(String[] args)
        {
    
            CountryInfo countryInfo = new CountryInfo();
            List<String> countryList = countryInfo.getCountryList(null);
    
            System.out.println("countryList size : " + countryList.size());
    
        }
    
    }
    
    Output
    countryList size : 0
    
    To Download CollectionsDemo-EmptyList-Country Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-EmptyList-Country.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
  • Java : Collection Framework : Collections (Min Comparator)


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

    Click the below Image to Enlarge
    Java : Collection Framework : Collections (Min Comparator) 
    Java : Collection Framework : Collections (Min Comparator) 
    Employee.java

    public class Employee
    {
        private int    employeeId;
        private String name;
    
        public Employee( int employeeId, String name )
        {
            super();
            this.employeeId = employeeId;
            this.name = name;
        }
    
        public int getEmployeeId()
        {
            return employeeId;
        }
    
        public void setEmployeeId( int employeeId )
        {
            this.employeeId = employeeId;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        @Override
        public String toString()
        {
            return "Employee [employeeId=" + employeeId + ", name=" + name + "]";
        }
    
    }
    
    EmployeeIdComparator.java
    import java.util.Comparator;
    
    public class EmployeeIdComparator implements Comparator<Employee>
    {
    
        /*
         * This method used to order the employee objects in Ascending based on employeeId.
         */
        @Override
        public int compare(Employee employee1, Employee employee2)
        {
    
            if (employee1.getEmployeeId() < employee2.getEmployeeId())
            {
                return -1;
            }
            else
            {
                return 1;
            }
    
        }
    
    }
    
    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     Method: 
    
     public static <T> T min(Collection<? extends T> coll,
                                           Comparator<? super T> comp)
    
     Parameters:
    
     coll - the collection whose minimum element is to be determined.
     comp - the comparator with which to determine the minimum element. 
            A null value indicates that the elements' natural ordering 
            should be used.
    
     Returns:
    
     the minimum element of the given collection, according to the specified comparator.
    
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
    
            ArrayList<Employee> employeeList = new ArrayList<Employee>();
    
            Employee john = new Employee(3, "John");
            Employee david = new Employee(2, "David");
            Employee peter = new Employee(1, "Peter");
            Employee juli = new Employee(5, "Juli");
            Employee ram = new Employee(4, "Ram");
    
            employeeList.add(john);
            employeeList.add(david);
            employeeList.add(peter);
            employeeList.add(juli);
            employeeList.add(ram);
    
            System.out.println("employeeList : " + employeeList + "\n");
    
            EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();
    
            /*
             * Returns the minimum element of the given collection, according to the
             * order induced by the specified comparator.
             */
            Employee employee = Collections.min(employeeList, employeeIdComparator);
    
            System.out.println("Min employee object : " + employee);
    
        }
    }
    
    
    Output
    employeeList : [Employee [employeeId=3, name=John], Employee [employeeId=2, name=David], 
    Employee [employeeId=1, name=Peter], Employee [employeeId=5, name=Juli], 
    Employee [employeeId=4, name=Ram]]
    
    Min employee object : Employee [employeeId=1, name=Peter]
    
    To Download CollectionsDemo-MinComparator Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-MinComparator.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
  • Java : Collection Framework : Collections Sorting - Playlist

    Java : Collection Framework : Collections (Max Comparator)


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

    Click the below Image to Enlarge
    Java : Collection Framework : Collections (Max Comparator)
    Java : Collection Framework : Collections (Max Comparator)
    Employee.java

    public class Employee
    {
        private int    employeeId;
        private String name;
    
        public Employee( int employeeId, String name )
        {
            super();
            this.employeeId = employeeId;
            this.name = name;
        }
    
        public int getEmployeeId()
        {
            return employeeId;
        }
    
        public void setEmployeeId( int employeeId )
        {
            this.employeeId = employeeId;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        @Override
        public String toString()
        {
            return "Employee [employeeId=" + employeeId + ", name=" + name + "]";
        }
    
    }
    
    EmployeeIdComparator.java
    import java.util.Comparator;
    
    public class EmployeeIdComparator implements Comparator<Employee>
    {
    
        /*
         * This method used to order the employee objects in Ascending based on employeeId.
         */
        @Override
        public int compare(Employee employee1, Employee employee2)
        {
    
            if (employee1.getEmployeeId() < employee2.getEmployeeId())
            {
                return -1;
            }
            else
            {
                return 1;
            }
    
        }
    
    }
    
    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     Method: 
    
     public static <T> T max(Collection<? extends T> coll,
                                        Comparator<? super T> comp)
    
     Parameters:
    
     coll - the collection whose maximum element is to be determined.
     comp - the comparator with which to determine the maximum element. 
            A null value indicates that the elements' natural ordering 
            should be used.
    
     Returns:
    
     the maximum element of the given collection, according to the 
     specified comparator.
    
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
    
            ArrayList<Employee> employeeList = new ArrayList<Employee>();
    
            Employee john = new Employee(3, "John");
            Employee david = new Employee(2, "David");
            Employee peter = new Employee(1, "Peter");
            Employee juli = new Employee(5, "Juli");
            Employee ram = new Employee(4, "Ram");
    
            employeeList.add(john);
            employeeList.add(david);
            employeeList.add(peter);
            employeeList.add(juli);
            employeeList.add(ram);
    
            System.out.println("employeeList : " + employeeList + "\n");
    
            EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();
            
            /*
             * Returns the maximum element of the given collection, according to the
             * order induced by the specified comparator.
             */
            Employee employee = Collections.max(employeeList,
                    employeeIdComparator);
    
            System.out.println("employee object whose employee id is max : " + employee);
            
        }
    }
    
    Output
    employeeList : [Employee [employeeId=3, name=John], Employee [employeeId=2, name=David], 
    Employee [employeeId=1, name=Peter], Employee [employeeId=5, name=Juli], 
    Employee [employeeId=4, name=Ram]]
    
    employee object whose employee id is max : Employee [employeeId=5, name=Juli]
    
    To Download CollectionsDemo-MaxComparator Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-MaxComparator.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
  • Thursday, 9 July 2015

    Java : Collection Framework : Collections (BinarySearch Comparator)


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

    Click the below Image to Enlarge
    Java : Collection Framework : Collections (BinarySearch Comparator) 
    Java : Collection Framework : Collections (BinarySearch Comparator) 
    Employee.java
    public class Employee
    {
        private Integer    employeeId;
        private String name;
    
        public Employee( Integer employeeId, String name )
        {
            super();
            this.employeeId = employeeId;
            this.name = name;
        }
    
        public Integer getEmployeeId()
        {
            return employeeId;
        }
    
        public void setEmployeeId( int employeeId )
        {
            this.employeeId = employeeId;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        @Override
        public String toString()
        {
            return "Employee [employeeId=" + employeeId + ", name=" + name + "]";
        }
    
    }
    
    EmployeeIdComparator.java
    import java.util.Comparator;
    
    public class EmployeeIdComparator implements Comparator<Employee>
    {
    
        /*
         * This method used to order the employee objects in Ascending based on employeeId.
         */
        @Override
        public int compare(Employee employee1, Employee employee2)
        {
    
            return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
    
        }
    
    }
    
    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    /*
     Method: 
    
     public static <T> int binarySearch(List<? extends T> list,
                                             T key,
                                             Comparator<? super T> c)
    
     Parameters:
    
     list - the list to be searched.
     key - the key to be searched for.
     c - the comparator by which the list is ordered. 
         A null value indicates that the elements' natural ordering should be used.
    
     Returns:
     
     the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). 
     The insertion point is defined as the point at which the key would be inserted into the list: 
     the index of the first element greater than the key, or list.size() 
     if all elements in the list are less than the specified key. 
     Note that this guarantees that the return value will be >= 0 if and only if the key is found.
    
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
    
            List<Employee> employeeList = new ArrayList<Employee>();
    
            Employee john = new Employee(300, "John");
            Employee david = new Employee(103, "David");
            Employee peter = new Employee(208, "Peter");
            Employee juli = new Employee(202, "Juli");
            Employee ram = new Employee(102, "Ram");
    
            employeeList.add(john);
            employeeList.add(david);
            employeeList.add(peter);
            employeeList.add(juli);
            employeeList.add(ram);
    
            System.out.println("employeeList before sort: \n" + employeeList + "\n");
    
            EmployeeIdComparator employeeIdComparator = new EmployeeIdComparator();
    
            Collections.sort(employeeList, employeeIdComparator);
    
            System.out
                    .println("employeeList after sort : \n" + employeeList + "\n");
    
            /*
             * Searches the specified list for the specified object using the binary
             * search algorithm. The list must be sorted into ascending order
             * according to the specified comparator (as by the sort(List,
             * Comparator) method), prior to making this call.
             * 
             * If it is not sorted,the results are undefined. If the list contains
             * multiple elements equal to the specified object, there is no
             * guarantee which one will be found.
             */
            
            Employee searchKey = new Employee(202, null); 
            
            int index = Collections.binarySearch(employeeList,
                    searchKey, employeeIdComparator);
    
            System.out.println("index : " + index);
            
            Employee employee = employeeList.get(index);        
            System.out.println("employee name in  '"+index+"' index Position : "
                    + employee.getName());
    
        }
    }
    
    Output
    employeeList before sort: 
    [Employee [employeeId=300, name=John], Employee [employeeId=103, name=David], Employee [employeeId=208, name=Peter], Employee [employeeId=202, name=Juli], Employee [employeeId=102, name=Ram]]
    
    employeeList after sort : 
    [Employee [employeeId=102, name=Ram], Employee [employeeId=103, name=David], Employee [employeeId=202, name=Juli], Employee [employeeId=208, name=Peter], Employee [employeeId=300, name=John]]
    
    index : 2
    employee name in  '2' index Position : Juli
    
    
    To Download CollectionsDemo-BinarySearchComparator Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-BinarySearchComparator.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
  • Java : Collection Framework : Collections (ReverseOrder Comparator)


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

    Employee.java
    public class Employee
    {
        private Integer    employeeId;
        private String name;
    
        public Employee( Integer employeeId, String name )
        {
            super();
            this.employeeId = employeeId;
            this.name = name;
        }
    
        public Integer getEmployeeId()
        {
            return employeeId;
        }
    
        public void setEmployeeId( int employeeId )
        {
            this.employeeId = employeeId;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName( String name )
        {
            this.name = name;
        }
    
        @Override
        public String toString()
        {
            return "Employee [employeeId=" + employeeId + ", name=" + name + "]";
        }
    
    }
    
    AscEmployeeIdComparator.java
    import java.util.Comparator;
    
    public class AscEmployeeIdComparator implements Comparator<Employee>
    {
    
        /*
         * This method used to order the employee objects in Ascending order based
         * on employeeId.
         */
        @Override
        public int compare(Employee employee1, Employee employee2)
        {
    
            return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
    
        }
    
    }
    
    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /*
     Method 
    
     public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)
    
     Parameters:
    
     cmp - a comparator who's ordering is to be reversed by the returned comparator or null
    
     Returns:
    
     A comparator that imposes the reverse ordering of the specified comparator.
    
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
    
            List<Employee> employeeList = new ArrayList<Employee>();
    
            Employee john = new Employee(300, "John");
            Employee david = new Employee(100, "David");
            Employee peter = new Employee(200, "Peter");
            Employee juli = new Employee(500, "Juli");
            Employee ram = new Employee(400, "Ram");
    
            employeeList.add(john);
            employeeList.add(david);
            employeeList.add(peter);
            employeeList.add(juli);
            employeeList.add(ram);
    
            System.out.println("Before sort the employeeList : \n");
            displayEmployeeInfo(employeeList);
    
            AscEmployeeIdComparator ascEmployeeIdComparator = new AscEmployeeIdComparator();
    
            Collections.sort(employeeList, ascEmployeeIdComparator);
    
            System.out
                    .println("\nAfter Sort the employeeList using ascEmployeeIdComparator : \n");
            displayEmployeeInfo(employeeList);
    
            /*
             * Returns a comparator that imposes the reverse ordering of the
             * specified comparator. If the specified comparator is null, this
             * method is equivalent to reverseOrder() (in other words, it returns a
             * comparator that imposes the reverse of the natural ordering on a
             * collection of objects that implement the Comparable interface).
             */
    
            Comparator<Employee> reverseEmployeeIdcomparator = Collections
                    .reverseOrder(ascEmployeeIdComparator);
    
            // sort the list
            Collections.sort(employeeList, reverseEmployeeIdcomparator);
    
            System.out
                    .println("\nAfter Sort the employeeList using reverseEmployeeIdcomparator : \n");
            displayEmployeeInfo(employeeList);
    
        }
    
        private static void displayEmployeeInfo(List<Employee> employeeList)
        {
            for (Employee employee : employeeList)
            {
                System.out.println("ID :" + employee.getEmployeeId() + " Name :"
                        + employee.getName());
    
            }
        }
    }
    
    Output
    Before sort the employeeList : 
    
    ID :300 Name :John
    ID :100 Name :David
    ID :200 Name :Peter
    ID :500 Name :Juli
    ID :400 Name :Ram
    
    After Sort the employeeList using ascEmployeeIdComparator : 
    
    ID :100 Name :David
    ID :200 Name :Peter
    ID :300 Name :John
    ID :400 Name :Ram
    ID :500 Name :Juli
    
    After Sort the employeeList using reverseEmployeeIdcomparator : 
    
    ID :500 Name :Juli
    ID :400 Name :Ram
    ID :300 Name :John
    ID :200 Name :Peter
    ID :100 Name :David
    
    To Download CollectionsDemo-ReverseOrderComparator Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-ReverseOrderComparator.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
  • Java : Collection Framework : Collections (ReverseOrder Comparator null)


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

    CollectionsExample.java
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.LinkedList;
    
    /*
     Method 
    
     public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)
    
     Parameters:
    
     cmp - a comparator who's ordering is to be reversed by the returned comparator or null
    
     Returns:
    
     A comparator that imposes the reverse ordering of the specified comparator.
    
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
    
            LinkedList<Integer> linkedList = new LinkedList<Integer>();
    
            linkedList.add(2);
            linkedList.add(1);
            linkedList.add(4);
            linkedList.add(3);
    
            System.out.println("linkedList before Sort " + linkedList+"\n");
    
            /*
             * Returns a comparator that imposes the reverse ordering of the
             * specified comparator. If the specified comparator is null, this
             * method is equivalent to reverseOrder() (in other words, it returns a
             * comparator that imposes the reverse of the natural ordering on a
             * collection of objects that implement the Comparable interface).
             */
            Comparator<Integer> reverseOrderComparator = Collections.reverseOrder(null);
    
            // sort the list
            Collections.sort(linkedList, reverseOrderComparator);
    
            System.out.println("linkedList sorted in ReverseOrder: " + linkedList);
    
        }
    }
    
    Output
    linkedList before Sort [2, 1, 4, 3]
    
    linkedList sorted in ReverseOrder: [4, 3, 2, 1]
    
    To Download CollectionsDemo-ReverseOrderNullComparator Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-ReverseOrderNullComparator.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
  • 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
  • Tutorials