Program to demonstrate Iterator Interface to traverse a Java Collection
Output of the program :
package com.hubberspot.collections.example; import java.util.*; public class IteratorExample { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("One"); list.add("Two"); list.add("Three"); list.add("Four"); Iterator iterate = list.iterator(); while(iterate.hasNext()) { String element = (String)iterate.next(); System.out.println("Element = " + element); iterate.remove(); } System.out.println("Array list contains" + list); } }
Output of the program :