1.

What Is An Iterator?

Answer»

Iterator is an INTERFACE which is part of the Java Collections FRAMEWORK. An Iterator enables you to iterate a collection. Iterators also allow the caller to remove elements from the underlying collection during the iteration.

Collection CLASSES provide iterator method which RETURNS an iterator. This iterator can be used to traverse a collection.

As exp. if there is a set called citySet, an iterator on this set can be obtained as -

Iterator<String> itr = citySet.iterator();
while (itr.hasNext()) {
String city = (String) itr.next();
System.out.println("city " + city);
}

Iterator is an interface which is part of the Java Collections framework. An Iterator enables you to iterate a collection. Iterators also allow the caller to remove elements from the underlying collection during the iteration.

Collection classes provide iterator method which returns an iterator. This iterator can be used to traverse a collection.

As exp. if there is a set called citySet, an iterator on this set can be obtained as -

Iterator<String> itr = citySet.iterator();
while (itr.hasNext()) {
String city = (String) itr.next();
System.out.println("city " + city);
}



Discussion

No Comment Found