An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired.
Collection Interface iterator() :
An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired. You get an Iterator for a collection by calling its iterator method.
Example :
import java.util.LinkedList; import java.util.Collection; import java.util.Iterator; public class collectioninterfaceiterator { public static void main(String[] args) { Collection collection = new LinkedList(); collection.add("Apple"); collection.add("Mango"); collection.add("Banana"); Iterator iter = collection.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } }
Output :
Apple Mango Banana |
[ 0 ] Comments