• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:


In vectors, what is the difference between using a get() and using a iterator(). which is better?. I have used both of them.



1 Solution
Jim CakalicSenior Developer/ArchitectCommented:

Vector implements the RandomAccess interface (as of JDK 1.4) so it is slightly more performant to use get with a for loop than it is to use iteration.

Performance issues aside, there might be other concerns that affect your decision. It is considered good design with collections to rely upon the most abstract interface that makes sense. In this case, it might be List or Collection. The reason is that you might one day choose to replace the concrete collection class with something else. For example, if you one day decided you needed to replace your Vector with a LinkedList then your choice to use get instead of iteration would cause a degradation in performance as LinkedLists are expensive to access in a RandomAccess manner.

There's also the possible concern of concurrent access and perchance modification of the collection while iterating. If two threads are accessing the Vector, one looping across the contents and another modifying, the for loop construction will sometimes fail, usually in a manner that is difficult to diagnose. The collection iterators are fail-fast so they will detect the concurrent modification and throw an explicit exception that can be caught and handled.

Jim Cakalic
eugene007Author Commented:
so the get() method access the vector in a random access manner. what about iterator() how does it access the vector?.
Iterator is a way to traverse through a collection,
usually by a natural sequence.
Mayank SAssociate Director - Product EngineeringCommented:
AFAIK, the iterator will be invalidated and it might throw an exception if another thread modifies the collection.
Jim CakalicSenior Developer/ArchitectCommented:
Hi, eugene007.

I think you have it turned a bit askew. Vector implements the RandomAccess interface -- just a marker interface -- to indicate that its implementation allows random access to elements using the get method in an efficient manner. Vector uses an Object[] to store its elements. You can directly index into the Object[] to retrieve any element with no time penalty. Contrast this with LinkedList which does not implement the RandomAccess interface because it is inefficient to use get to retrieve elements from the collection. LinkedList stores its elements as a series of connected nodes. To get to a specific element LinkedList must start at the head (1st element) and traverse all the intervening elements until it arrives at the one you requested. Accessing the last element requires going through all the other elements to get there.

How an iterator accesses the underlying collection is implementation specific. The Vector Iterator implementation keeps an index of the current element in the collection. Each time you call iterator.next() it retrieves the current element using the Vector get method, increments the index, and returns the element. The LinkedList Iterator actually keeps a reference to the node in the list that is next. When its iterator.next() is called, it sets the next reference to the node pointed to be the current node and then returns the element stored at the current node. Similarly, previous moves the next reference "backwards". The get method is not used by the LinkedList Iterator because it would be inefficient.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now