aspose file tools*
The moose likes Java in General and the fly likes iterator vs collection's getter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "iterator vs collection Watch "iterator vs collection New topic
Author

iterator vs collection's getter

Lidia Cyc
Greenhorn

Joined: Aug 05, 2003
Posts: 25
hey all,
let's say i have a vector and i wanna iterate through it until the needed
object in this vector is found.
i tried both : an iterator and a loop with vector's getter . the second
choice looks much faster.
if the iterator is not as fast as getter, why would one use it?
thanks
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

There are a few reasons that I can think of, other probably can tell more.
1. Abstraction. Iterator work with all Collection, which consist of List and Set interface and their implementations. getter only work with List only. Same reason it is suggested that you declare your variable as Collection object instead of instance of ArrayList or Vector for example. So you can switch implementation easily.
2. Safety reason. with direct access using get() and index, it is easier to make mistake and pas invalid/out-of-bound index.
3. Extra features. Iterator and ListIterator has other methods that help to do other thing beside traversing the collection, like inserting or removing object. So you don't have to do it manually by calculating the index, etc.
Check out this post, Jim has more explanation:
more on iterator
[ October 05, 2003: Message edited by: Wirianto Djunaidi ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
normally you use an iterator if you want to iterate over the complete content of a collection etc. If you know which element to take and the class is providing a getter method then take the getter method.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Wirianto Djunaidi:
getter only work with List only.

And on a LinkedList, using an iterator actually is *much* faster.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Iterator also provides some safety around remove. If you're using a positional index to get items and remove one in the middle of your loop, the positions of some items change.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
An iterator also provides fail-fast behavior if the list you're iterating over is sturcturally modified while you're iterating. (I.e. if another thread adds or removes elemenst from the collection.) Fail-fast means that if the iterator detects that the list is being modified by another thread while you're iterating, it will promptly throw a ConcurrentModificationException. This is usually a good thing, because it make it easier for you to detect the problem and fix it (typically by introducing synchronization, or iterating over a copy of the collection rather than the original). If you use get() instead, you may well have a program that functions correctly 50%, 90%, maybe 99% of the time - but then it will fail in strange and mysterious ways. The failures may be hard to preproduce or trace the cause of. If my program is going to have a bug, I'd rather it be a big obvious bug that I can find easily, rather than a small subtle bug the appears unpredictably. So to me, this is a big advantage to using an Iterator when dealing with a Collection in a multi-threaded environment (or an environment that may become multi-threaded later; this can be hard to anticipate).


"I'm not back." - Bill Harding, Twister
 
Consider Paul's rocket mass heater.
 
subject: iterator vs collection's getter