Java Generic Iterator


 

Java Generic Iterator

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

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

Ads