Wednesday, 8 January 2014

Iterable interface and Iterator interface

Iterable interface is in  java.lang  package and  Iterator interface in  java.util package

public interface Iterable

Implementing this interface allows an object to be the target of the "foreach" statement.
This interface has one method :

iterator(), Returns an iterator over a set of elements.








The implementing classes for this interface is AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet e.t.c

also iterator() is implemented in classes ArrayList , HashSet and few more classes.

Basically ArrayList override iterator() from AbstractList, 
and in same way HashSet override iterator() from AbstractSet

 public interface Iterator

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
  • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  • Method names have been improved.
This interface is a member of the Java Collections Framework.

This interface has three methods :

hasNext(),
next(),
remove()









No comments:

Post a Comment