How to use Iterator in Java? Last Updated : 18 Jul, 2018 Comments Improve Suggest changes Like Article Like Report 'Iterator' is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection. java.util package has public interface Iterator and contains three methods: boolean hasNext(): It returns true if Iterator has more element to iterate. Object next(): It returns the next element in the collection until the hasNext()method return true. This method throws 'NoSuchElementException' if there is no next element. void remove(): It removes the current element in the collection. This method throws 'IllegalStateException' if this function is called before next( ) is invoked. Java // Java code to illustrate the use of iterator import java.io.*; import java.util.*; class Test { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); // Iterator to traverse the list Iterator iterator = list.iterator(); System.out.println("List elements : "); while (iterator.hasNext()) System.out.print(iterator.next() + " "); System.out.println(); } } Output: List elements : A B C D E ListIterator 'ListIterator' in Java is an Iterator which allows users to traverse Collection in both direction. It contains the following methods: void add(Object object): It inserts object immediately before the element that is returned by the next( ) function. boolean hasNext( ): It returns true if the list has a next element. boolean hasPrevious( ): It returns true if the list has a previous element. Object next( ): It returns the next element of the list. It throws 'NoSuchElementException' if there is no next element in the list. Object previous( ): It returns the previous element of the list. It throws 'NoSuchElementException' if there is no previous element. void remove( ): It removes the current element from the list. It throws 'IllegalStateException' if this function is called before next( ) or previous( ) is invoked. Java // Java code to illustrate the use of ListIterator import java.io.*; import java.util.*; class Test { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); // ListIterator to traverse the list ListIterator iterator = list.listIterator(); // Traversing the list in forward direction System.out.println("Displaying list elements in forward direction : "); while (iterator.hasNext()) System.out.print(iterator.next() + " "); System.out.println(); // Traversing the list in backward direction System.out.println("Displaying list elements in backward direction : "); while (iterator.hasPrevious()) System.out.print(iterator.previous() + " "); System.out.println(); } } Output: Displaying list elements in forward direction : A B C D E Displaying list elements in backward direction : E D C B A Related Articles: Iterators in Java Iterator vs Foreach In Java Retrieving Elements from Collection in Java (For-each, Iterator, ListIterator & EnumerationIterator) Comment More infoAdvertise with us Next Article How to use Iterator in Java? M Mehak Narang Improve Article Tags : Java Java-Collections Practice Tags : JavaJava-Collections Similar Reads TreeSet iterator() Method in Java The Java.util.TreeSet.iterator() method is used to return an iterator of the same elements as that of the TreeSet. The elements are returned in random order from what was present in the Tree set. Syntax: Iterator iterate_value = Tree_Set.iterator(); Parameters: The function does not take any paramet 1 min read Iterator vs Foreach In Java Background : 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 iterator for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next()); For eachloop is meant f 4 min read How to Iterate Any Map in Java? In Java, a Map is a data structure that is used to store key-value pairs. Understanding how to iterate over the elements of a map plays a very important role. There are 5 ways to iterate over the elements of a map, and in this article, we are going to discuss all of them.Note: We cannot iterate over 5 min read How to iterate over a TreeMap in Java? Given a TreeMap, the task is to iterate this TreeMap in Java. The TreeMap in Java is used to implement Map interface and NavigableMap along with the Abstract Class. We cannot iterate a TreeMap directly using iterators, because TreeMap is not a Collection. So we will have to use TreeMap.entrySet() me 3 min read Deque iterator() method in Java The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a âweakly consistentâ iterator. Syntax: Iterator iterator() Parameters: This method doe 3 min read NavigableSet iterator() method in Java The iterator() method of NavigableSet interface in Java is used to return an iterator over the elements in this set, in ascending order. This iterator can be then used to iterate over the elements of the set. Syntax: Iterator<E> iterator() Where, E is the type of elements maintained by this Se 2 min read Iterator vs Collection in Java Iterator and Collection, both has helped and comforted the programmers at many a times. But their usage and application has a very wide difference. 1. Iterator Declaration public interface Iterator Type Parameters: E - the type of elements returned by this iteratorIterators are used in Collection fr 3 min read PriorityQueue iterator() Method in Java The Java.util.PriorityQueue.iterator() method is used to return an iterator of the same elements as the Priority Queue. The elements are returned in random order from what present in the Queue. Syntax: Iterator iterate_value = Priority_Queue.iterator(); Parameters: The function does not take any par 2 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Convert Iterator to Iterable in Java Given an Iterator, the task is to convert it into Iterables in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: By overriding the abstract method Iterable.itera 3 min read Like