In this Java tutorial we will see how to use the iterator interface with Collection. We will create an example to display the contents of the Collection.
In this Java tutorial we will see how to use the iterator interface with Collection. We will create an example to display the contents of the Collection.
Example of Java Collection Iterator
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class collection2 { public static void main(String[] args) { Collection c = new ArrayList(); c.add("rose"); c.add("Lotus"); c.add("Marigold"); c.add("Jasmine"); Iterator ir = c.iterator(); while (ir.hasNext()) { System.out.println(ir.next()); } } }
Output
rose Lotus Marigold Jasmine