Iterator : Java Glossary

go to home page I words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-06-25 Roedy Green, Canadian Mind Products
Iterator
Iterators let you process each element of a Collection. Iterators are a nice vanilla, generic way to go through all the elements of a Collection no matter how it is organised. Iterator is an Interface implemented a different way for every Collection. Iterator has replaced the older Enumeration interface.

Iterable is the interface that notes the class has a iterator method capable of emitting a one-shot Iterator object. Since Iterable in a superinterface of Collection, all Collections are Iterable, but not the reverse. The for:each shorthand loop syntax works only on classes than implement Iterable.

Sample Code Iterable
Generics Learning More
Implementing Links
Remove

Sample Code

Note how Iterators deliver raw Objects with the next method. It is up to you to cast them back to what they really are.

Generics

With generics, Iterators are typed, and hence you don’t need the cast to fish each object out of them. Further, you have access to the for:each loop.

Implementing

Here is how a simple Iterator works inside:
Here is how a more realistic one works that implements ListIterator.
Iterators optionally implement the remove method or just leave a dummy stub that triggers an exception.

Remove

While in the middle of using an Iterator, if you try to add or remove elements, with Collection methods, you will get a ConcurrentModificationException. You can add and remove elements if you use Iterator. remove or ListIterator. add. The alternative is to create a todo Collection during the iteration pass and then add or delete the elements when you are done.

Deleting a large number of elements from a large ArrayList will run faster if you create a new ArrayList just the right size, then populate it leaving out the undesirable elements. This avoids the repeated sliding down of all elements above the deleted one. Also processing an ArrayList in descending order to delete is more efficient since remove will not need to slide elements that will be soon deleted.

Here is some sampe code to use Iterator.remove.

Iterable

You might want to make your own class implement Iterable, so that you can use it in a for:each statement.

Learning More

Sun’s Javadoc on Iterator interface : available:
Sun’s Javadoc on ListIterator interface : available:
Sun’s Javadoc on Iterable interface : available:
Sun’s Javadoc on Enumeration interface : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/iterator.html J:\mindprod\jgloss\iterator.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.109]
You are visitor number 56,721.