Iterator in java
Iterator in java allows to cycle through a collection can be added or removing elements. To access a collection over an iterator, must attain one. Each of the collection classes offers an iterator () method that returns an iterator to the start of the collection. By using this iterator object can access each element in the collection at a time each element. Class ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements. In general, to use an iterator to cycle through the contents of a collection steps are:
- Obtain an iterator to the start of the collection by calling the collection’s iterator() method.
- Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true.
- Within the loop, obtain each element by calling next( ).
The Methods Declared by Iterator
Method with Description mention below
- hasNext( ) returns true if there are more elements. Otherwise, returns false.
- Object next( ) returns the next element. Throws NoSuchElementException if there is not a next element.
- remove( ) method is used to removes the current element. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded by a call to next( ).
The Methods Declared by ListIterator
- add(Object obj): It is used to Inserts obj into the list in front of the element that will be returned by the next call to next( ).
- hasNext( ): It returns true if there is a next element. Otherwise, returns false.
- hasPrevious( ):It returns true if there is a previous element. Otherwise, returns false.
- Object next( ): It returns the next element. A NoSuchElementException is thrown if there is not a next element.
- nextIndex( ):It returns the index of the next element. If there is not a next element, returns the size of the list.
- Object previous( ): This is returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
- previousIndex( ):This is returns the index of the previous element. If there is not a previous element, returns -1.
- remove( ): It is removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked.
- set(Object obj): This is used to assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).
Example
import java.util.*; public class Main { public static void main(String args[]) { ArrayList letters = new ArrayList();// Create an array list // add elements to the array list letters.add("C"); letters.add("A"); letters.add("E"); letters.add("B"); letters.add("D"); letters.add("F"); // Use iterator to display contents of letters System.out.print("Original contents of letters: "); Iterator itr_1 = letters.iterator(); while(itr_1.hasNext()) { Object object = itr_1.next(); System.out.print(object + " "); } System.out.println(); // Modify objects being iterated ListIterator itr_2 = letters.listIterator(); while(itr_2.hasNext()) { Object object1 = itr_2.next(); itr_2.set(object1 + "+"); } System.out.print("Modified contents of letters: "); itr_1 = letters.iterator(); while(itr_1.hasNext()) { Object object = itr_1.next(); System.out.print(object + " "); } System.out.println(); // Now, display the list backwards System.out.print("Modified list backwards: "); while(itr_2.hasPrevious()) { Object object = itr_2.previous(); System.out.print(object + " "); } System.out.println(); } }
This will produce the following result −
Output
Original contents of al: C A E B D F
Modified contents of al: C+ A+ E+ B+ D+ F+
Modified list backwards: F+ D+ B+ E+ A+ C+