All Iterator, ListIterator, and Enumeration represents cursors in java which are used to iterate over collection elements. But they have some differences also. Let us discuss each with example, when to use which, and differences.
Iterator
Iterator interface present in java.util package. It is used to read or remove the collection elements by iterating over specified collection.
Note: Iterator interface can be applied to all collection classes but it can traverse only in single direction that is Forward direction.
The iterator() method of Collection interface is used to get the Iterator interface.
Iterator iterator = collection.iterator();
Example
import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { // Creating ArrayList ArrayList subjects = new ArrayList(); // Adding elements to ArrayList subjects.add("Java"); subjects.add("Servlet"); subjects.add("JSP"); subjects.add("Spring"); subjects.add("Hibernet"); // Get Iterator Iterator iterator = subjects.iterator(); // Print ArrayList elements while (iterator.hasNext()){ System.out.println(iterator.next()); } } } |
Output
Java Servlet JSP Spring Hibernet |
ListIterator
Iterator interface present in java.util package. It extends java.util.Iterator interface. It is used to read, remove, add, and update the collection elements by iterating over specified List type collection.
Note: Iterator interface can be applicable only on List objects like ArrayList or LinkedList but it can traverse in both directions that is Forward as well as Backward direction.
The listIterator() method of List interface is used to get the ListIterator object.
ListIterator listIterator = list.listIterator();
Example
import java.util.ArrayList; import java.util.ListIterator; public class Main { public static void main(String[] args) { // Creating ArrayList ArrayList subjects = new ArrayList(); // Adding elements to ArrayList subjects.add("Java"); subjects.add("Servlet"); subjects.add("JSP"); subjects.add("Spring"); subjects.add("Hibernet"); // Get ListIterator ListIterator listIterator = subjects.listIterator(); // Print ArrayList elements in Forward Direction System.out.println("ArrayList elements in Forward Direction"); while (listIterator.hasNext()){ System.out.println(listIterator.next()); } // Print ArrayList elements in Backward Direction System.out.println("ArrayList elements in Backward Direction"); while (listIterator.hasPrevious()){ System.out.println(listIterator.previous()); } } } |
Output
ArrayList elements in Forward Direction Java Servlet JSP Spring Hibernet ArrayList elements in Backward Direction Hibernet Spring JSP Servlet Java |
Enumeration
Enumeration interface present in java.util package. It is used to read collection elements by iterating over specified legacy collection like Vector, Properties, Hashtable, Stack, and Dictionary abstract class.
Note: Iterator interface can be applied to Vector, Hashtable, Stack, Properties and Dictionary abstract class and it can traverse only in Forward direction. We can not perform any operation other then read or get.
The elements() method of legacy Collection classes is used to get the Enumeration object.
Vector vector = new Vector(); Enumeration enumeration = vector.elements();
Example
import java.util.Enumeration; import java.util.Vector; public class Main { public static void main(String[] args) { // Creating Vector Vector subjects = new Vector(); // Adding elements to Vector subjects.addElement("Java"); subjects.addElement("Servlet"); subjects.addElement("JSP"); subjects.addElement("Spring"); subjects.addElement("Hibernet"); // Get Enumeration Enumeration enumeration = subjects.elements(); // Print Vector elements while (enumeration.hasMoreElements()){ System.out.println(enumeration.nextElement()); } } } |
Output
Java Servlet JSP Spring Hibernet |
Difference between Iterator, ListIterator, and Enumeration
Enumeration | Iterator | ListIterator |
It is part of Legacy collection and introduced in Java 1.0 version. | It is part of Collection framework and introduced in Java 1.2 version | It is part of Collection framework and introduced in Java 1.2 version |
Only applicable on legacy classes like Hashtable, Vector, stack, Properties etc | It can be applied to any collection classes. | Only applicable for List objects like ArrayList , LinkedList, Vector. |
Can traverse only in FORWARD direction | Can traverse only in FORWARD direction | Can traverse in both FORWARD and BACKWARD directions. |
It can perform only read or get operation. | It can perform read as well as remove operation. | It can perform read, add, remove, and update operations. |
The elements() method of any legacy collection class can be used to get Enumeration objectFor example, Vector vector = new Vector(); Enumeration enumeration = vector.elements(); | The iterator() method of any collection class can be used to get Iterator object. For example, Iterator iterator= collection.iterator(); | The listIterator() method of any List classes can be used to get ListIterator objectFor example, ListIterator listIterator = list.listIterator(); |
What is the difference between arraylist and vector in java?Java interview questions on collections
- What is the difference between arraylist and linkedlist?
- What is the difference between Iterator and ListIterator?
- What is the difference between Iterator and Enumeration?
- what is the difference between list and set in java?
- what is the difference between set and map in java?
- what is the difference between hashset and treeset in java?
- what is the difference between hashset and hashmap in java?
- what is the difference between hashmap and treemap in java?
- what is the difference between hashmap and hashtable in java?
- what is the difference between collection and collections in java?
- what is the difference between comparable and comparator interfaces?
- what is the hashcode method in java?
- Java equals method
- Java hashCode method
- Why to override hashcode and equals method in java?
- How hashmap works intrnally?
- How put and get works in hashmap?
- How to resolve collision in hashmap?
- How hashmap stores null key?
- How hashset works intrnally?