Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

iterator vs collection's getter

 
Lidia Cyc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wirianto Djunaidi:
getter only work with List only.

And on a LinkedList, using an iterator actually is *much* faster.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic