Share on Google+Share on Google+

Java Generic Iterator

Advertisement
In this section of tutorial we will learn how to use the Generics with the Iterator interface. We will create an example using Generics in Iterator and ArrayList

  • Java Generic Iterator is the new feature added to the jdk1.5.
  • It is used to generalize the Collection classes.
  • It makes the Collection class type safe.
  • It is used with classes, methods and interfaces.


Java Generics Iterator Example
import java.util.ArrayList;
import java.util.Iterator;

public class generic {

	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		list.add("india");
		list.add("china");
		// list.add(2000);//gives error
		Iterator it = list.iterator();
		while (it.hasNext())
	         System.out.println(it.next());
	}
}

Output

india china

Advertisements

Posted on: November 3, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.