Prerequisite : Collection in Java
Following are the 4 ways to retrieve any elements from a collection object:
For each loop is meant for traversing items in a collection.
// Iterating over collection 'c' using for-each for (Element e: c) System.out.println(e);
We read the ‘:’ used in for-each loop as “in”. So loop reads as “for each element e in elements”, here elements is the collection which stores Element type items.
Note :In Java 8 using lambda expressions we can simply replace for-each loop with
elements.forEach (e -> System.out.println(e) );
Cursor is an interface and it is used to retrieve data from collection object,one by one. Cursor has 3 types, which are given below:
Iterator Interface: Iterator is an interface provided by collection framework to traverse a collection and for a sequential access of items in the collection.
// Iterating over collection 'c' using terator for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next());
It has 3 methods:
- boolean hasNext(): This method returns true if the iterator has more elements.
- elements next(): This method returns the next elements in the iterator.
- void remove(): This method removes from the collection the last elements returned by the iterator.
// Java program to demonstrate working of iterators
//create a Hashset to store strings
HashSet<String> hs =
// store some string elements
// Add an Iterator to hs.
Iterator it = hs.iterator();
// Display element by element using Iterator
America Japan India
Refer Iterator vs For-each for differences between iterator and for-each.
- ListIterator Interface: It is an interface that contains methods to retrieve the elements from a collection object, both in forward and reverse directions. This iterator is for list based collections.
It has following important methods:
- booleanhasNext(): This returns true if the ListIterator has more elements when traversing the list in the forward direction.
- booleanhasPrevious(): This returns true if the ListIterator has more elements when traversing the list in the reverse direction.
- element next(): This returns the next element in the list.
- element previous():This returns the previous element in the list.
- void remove(): This removes from the list the last elements that was returned by the next() or previous() methods.
- int nextIndex() Returns the index of the element that would be returned by a subsequent call to next(). (Returns list size if the list iterator is at the end of the list.)
- int previousIndex() Returns the index of the element that would be returned by a subsequent call to previous(). (Returns -1 if the list iterator is at the beginning of the list.)
Source: ListIterator Oracle Docs
How is Iterator different from ListIterator?
Iterator can retrieve the elements only in forward direction. But ListIterator can retrieve the elements in forward and reverse direction also. So ListIterator is preferred to Iterator.
Using ListItarator, we can get iterator’s current position
Since ListIterator can access elements in both directions and supports additional operators, ListIterator cannot be applied on Set (e.g., HashSet and TreeSet. See this). However, we can use LisIterator with vector and list (e.g. ArrayList ).
// Java program to demonstrate working of ListIterator
java. util.* ;
// take a vector to store Integer objects
Vector<Integer> v =
// Adding Elements to Vector
// Creating a ListIterator
ListIterator lit = v.listIterator();
"In Forward direction:"
"\n\nIn backward direction:\n"
EnumerationIterator Interface: The interface is useful to retrieve one by one the element. This iterator is based on data from Enumeration and has methods:
- booleanhasMoreElements(): This method tests if the Enumeration has any more elements or not .
- element nextElement(): This returns the next element that is available in elements that is available in Enumeration
What is the difference between Iterator and Enumeration?
Iterator has an option to remove elements from the collection which is not available in Enumeration. Also, Iterator has methods whose names are easy to follow and Enumeration methods are difficult to remember.
Vector dayNames =
// Creating enumeration
Enumeration days = dayNames.elements();
// Retrieving elements of enumeration
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
In Forward direction: 10 20 30 In backward direction: 30 20 10
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Difference between an Iterator and ListIterator in Java
- Enumeration vs Iterator vs ListIterator in Java
- Iterator vs Foreach In Java
- Iterator vs Collection in Java
- LinkedList listIterator() Method in Java
- Java AbstractSequentialList | ListIterator()
- CopyOnWriteArrayList listIterator() method in Java
- ArrayList listIterator() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- Stack listIterator(int) method in Java with Example
- Stack listIterator() method in Java with Example
- Vector listIterator() method in Java with Examples
- List listIterator() Method in Java with Examples
- && operator in Java with Examples
- Foreach in C++ and Java
- Stream forEach() method in Java with examples
- IntStream forEach() method in Java
- DoubleStream forEach() method in Java
- LongStream forEach() method in Java
Improved By : ManojbabuRTR