Iterators in Java

https://www.geeksforgeeks.org/iterators-in-java/

Enumeration

Enumeration e = v.elements();
hasMoreElements();
nextElement();

Limitations of Enumeration

  • only for legacy classes(Vector, Hashtable0 only. Hence it is not a universal iterator
  • remove operations can’t be performed using Enumeration
  • only forward direction iterating is possible

Iterator

It is a universal iterator as we can apply it to any Collection object.

// Here "c" is any Collection object.
Iterator itr = c.iterator();
hasNext();
next();
remove()

Limitations of Iterator

  • only forward direction iterating is possible
  • Replacement and addition of new element is not supported by Iterator.

猜你喜欢

转载自blog.csdn.net/qq_34446790/article/details/83109479