This post originated from an RSS feed registered with Java Buzz
by Javin Paul.
Original Post: Java - Iterator Example and Tutorial
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Java Iterator is used to iterate over Collections in Java. By Iteration,
I mean, going over each element stored in collection and optionally performing
some operation e.g. printing value of element, updating object or removing
object from Collection. Iterator
was not part of first Java release, and a similar class Enumeration was there
to provide Iteration functionality. Iterator in Java was introduced form JDK
1.4 and it provides alternative to Enumeration, which is obsolete now days.
Iterator is different to Enumeration in two main ways, first, Iterator allows
programmer to remove elements from Collection during iteration. Second, names
are shortened and improved in Iterator, by the way, you can see difference
between Iterator and Enumeration for more differences. It's one of the
frequently asked Java
Interview question. Iterator is an interface and enhanced to support
Generic from Java 1.5 release. Almost all popular collection implements
Iterator, including ArrayList, LinkedList and HashSet. hasNext() method of
Iterator is used as condition while Iterating, and next() method
actually returns object, next in sequence maintained by Collection itself. In this
Java programming tutorial, we will learn How to use Iterator in Java by coding
Iterator example and iterating over ArrayList.