Java Next Iterator


 

Java Next Iterator

This section of Java tutorial demonstrates how to use next method of the Iterator Interface. We will create an example to display the contents of the map with Iterator.

This section of Java tutorial demonstrates how to use next method of the Iterator Interface. We will create an example to display the contents of the map with Iterator.

  • Iterator is used by the set, List Interface and its subclasses.
  • Iterator has next method to get the next element.
  • Next method returns the object.
  • So it should be casted for further use.


Example of Java Next Iterator
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class next {
	public static void main(String[] args) {
		Integer ar[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
		ArrayList list = new ArrayList();
		for (Integer int1 : ar) {
			list.add(int1);
		}

		Iterator it = list.iterator();
		while (it.hasNext()) {
			Integer myint = (Integer) it.next();
			myint = myint + 9;
			System.out.print(myint + "\t");
		}
	}
}

Output

20 31 42 53 64 75 86 97 108

Ads