An Iterator is an interface that is used to fetch elements one by one in a collection. It is available in a Java package called Java. util package. The collection API implements the iterator() method, and hence data can be retrieved from interfaces like Map, List, Queue, Deque, and Set, which are all implemented from the collection framework. As the name suggests, an iterator in Java iterates through a collection of objects.
Syntax:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Iterator<E> iterator()
Below the iterator is the name of an object created by calling iterator() method of collection interface. “collection” is the collection object’s name.
Iterator iter = collection.iterator();
Iterators have 4 methods in Java that are used to traverse through collections and retrieve the required information. They are as follows:
Below is the example of Iterator in Java:
Code:
import java.io.*; import java.util.*; public class IteratorExample { public static void main(String[] args) { ArrayList<String> val = new ArrayList<String>(); val.add("Iteration started"); val.add("Printing iteration1"); val.add("Printing iteration2"); val.add("Printing iteration3"); val.add("End of iteration"); // Iterates through the list Iterator iter = val.iterator(); System.out.println("The values of iteration are as follows: "); while (iter.hasNext()) System.out.println(iter.next() + " "); System.out.println(); } }
Output:
In a list of elements, an iterator can fetch information only on the existing elements. Therefore, it will crash or throw an exception if tried to access an element that is not present in the next iteration. Here we shall get to know the different kinds of exceptions we get while implementing the iterator methods.
While iterating through a set of elements and fetching them by this method.
There are 2 kinds of exceptions that can occur here:
Syntax:
//ArrayList is created from the list having fixed size list = new ArrayList<String>(list); Iterator<String> iter = list.iterator(); while(iter.hasNext()){ if( iter.next().equals("First iteration") ){ iter.remove(); } }
These methods allow the iterator to traverse in both directions of the collection object. Following are some of them:
Below is an example in ArrayList for ListIterator.
Code:
import java.util.*; public class IteratorExample { public static void main(String args[]) { // Creating an array list ArrayList array = new ArrayList(); // add elements to the array list array.add("First element"); array.add("Second element"); array.add("Third element"); array.add("Fourth element"); array.add("Fifth element"); array.add("Sixth element"); // Displaying elements of an array System.out.println("Printing input of the array: "); Iterator iter = array.iterator(); while(iter.hasNext()) { Object value = iter.next(); System.out.println(value + " "); } System.out.println(); // To update the elements of iteration ListIterator listiter = array.listIterator(); while(listiter.hasNext()) { Object value = listiter.next(); listiter.set(value + "+"); } System.out.print("Updated array elements are as follows: "); iter = array.iterator(); while(iter.hasNext()) { Object value = iter.next(); System.out.print(value + " "); } System.out.println("\n"); // To display the contents in backward direction System.out.println("Printing elements in backward direction: "); while(listiter.hasPrevious()) { Object value = listiter.previous(); System.out.print(value + " "); } System.out.println(); } }
Output:
Below are the advantages of Iterators:
Below are the limitations of Iterators:
Iterators are the most commonly used method to retrieve elements from the collection interface. It is called Universal Java Cursor as it is applicable across all the Collection classes.
This is a guide to Iterator in Java. Here we discuss methods and examples of Iterator in Java along with its Limitations and Advantages. You can also go through our other suggested articles to learn more –
The above is the detailed content of Iterator in Java. For more information, please follow other related articles on the PHP Chinese website!