beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Iterator with examples

By Chaitanya Singh | Filed Under: Java Collections

Iterator is used for iterating (looping) various collection classes such as HashMap, ArrayList, LinkedList etc. In this tutorial, we will learn what is iterator, how to use it and what are the issues that can come up while using it. Iterator took place of Enumeration, which was used to iterate legacy classes such as Vector. We will also see the differences between Iterator and Enumeration in this tutorial.

Iterator without Generics Example

Generics got introduced in Java 5. Before that there were no concept of Generics.

import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorDemo1 {
 
  public static void main(String args[]){
    ArrayList names = new ArrayList();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");
 
    Iterator it = names.iterator();
 
    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }
  }
 
}

Output:

Chaitanya
Steve
Jack

In the above example we have iterated ArrayList without using Generics. Program ran fine without any issues, however there may be a possibility of ClassCastException if you don’t use Generics (we will see this in next section).

Read them too:
How to iterate HashMap
How to iterate LinkedList

Iterator with Generics Example

In the above section we discussed about ClassCastException. Lets see what is it and why it occurs when we don’t use Generics.

import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorDemo2 {
 
  public static void main(String args[]){
    ArrayList names = new ArrayList();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");
    
    //Adding Integer value to String ArrayList
    names.add(new Integer(10));
 
    Iterator it = names.iterator();
 
    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }
  }
}

Output:

ChaitanyaException in thread "main" 
Steve
Jack
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at beginnersbook.com.Details.main(Details.java:18)

In the above program we tried to add Integer value to the ArrayList of String but we didn’t get any compile time error because we didn’t use Generics. However since we type casted the integer value to String in the while loop, we got ClassCastException.

Use Generics:
Here we are using Generics so we didn’t type caste the output. If you try to add a integer value to ArrayList in the below program, you would get compile time error. This way we can avoid ClassCastException.

import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorDemo3 {
  public static void main(String args[]){
    ArrayList<String> names = new ArrayList<String>();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");
 
    Iterator<String> it = names.iterator();
 
    while(it.hasNext()) {
      String obj = it.next();
      System.out.println(obj);
    }
 }
}

Note: We did not type cast iterator returned value[it.next()] as it is not required when using Generics.

Difference between Iterator and Enumeration

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
1) Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
2) Method names have been improved. hashNext() method of iterator replaced hasMoreElements() method of enumeration, similarly next() replaced nextElement().

ConcurrentModificationException while using Iterator

import java.util.ArrayList;
public class ExceptionDemo {
  public static void main(String args[]){
     ArrayList<String> books = new ArrayList<String>();
     books.add("C");
     books.add("Java");
     books.add("Cobol");
 
     for(String obj : books) {
        System.out.println(obj); 
        //We are adding element while iterating list
        books.add("C++");
     }
  }
}

Output:

C
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
	at java.util.ArrayList$Itr.next(Unknown Source)
	at beginnersbook.com.Details.main(Details.java:12)

We cannot add or remove elements to the collection while using iterator over it.

Explanation From Javadoc:
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Enjoyed this post? Try these related posts

  1. Comparator Interface in Java
  2. Remove mapping from Hashtable example – Java
  3. How to clone an ArrayList to another ArrayList
  4. Difference between HashMap and Hashtable
  5. How to sort Hashtable in java
  6. Java – Remove specific elements from LinkedList example

Comments

  1. harikrishnc says

    March 9, 2015 at 6:48 PM

    i think while we can remove the element from the collection only we cannot add the element into the collection it will throw concurrentmodificationexception

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap