Posted on 2005-04-11
Last Modified: 2010-03-31
In vectors, what is the difference between using a get() and using a iterator(). which is better?. I have used both of them.



Question by:eugene007
    LVL 19

    Expert Comment

    by:Jim Cakalic

    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

    Author Comment

    so the get() method access the vector in a random access manner. what about iterator() how does it access the vector?.
    LVL 24

    Expert Comment

    Iterator is a way to traverse through a collection,
    usually by a natural sequence.
    LVL 30

    Expert Comment

    AFAIK, the iterator will be invalidated and it might throw an exception if another thread modifies the collection.
    LVL 19

    Accepted Solution

    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 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 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.


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now