Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Which is better Vector<type> or ArrayList ?

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Lightbulb Which is better Vector<type> or ArrayList ?

    Hello forum!

    I've learned the vector class by myself, since level 1 java programming at college. Right now in level 2 we are supposed to learn ArrayList, from what I've heard and read there isn't much of a difference between these two. Is that correct?

    So far, I'd like to do vectors more than learn about the arraylist, they're almost have identical methods and all. Except the constructors in vectors are more convenient.

    Some cares to answer?

    What if I went ahead and decided to write code 2-dimensional flexible matrix, ei. Vector<Vector> as an example, and maybe mask it with a different class name.

    Are there any practical differences?


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    78
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Which is better Vector<type> or ArrayList ?

    If im not mistaken, the main difference is that vector is thread safe. I may be wrong or slightly off on that, but im pretty sure thats it.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which is better Vector<type> or ArrayList ?

    As sci4me, Vector is internally "thread-safe" while ArrayList isn't. Personally I wouldn't ever use Vector because there is both too much and too little thread safety with Vector. You'll end up with code which runs slowly, and might not be correct.

  4. #4
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Which is better Vector<type> or ArrayList ?

    Well I am not doing anything fancy, or using any special methods from it.

    So whats all the thread-safe or un-safe deal about? Could you explain briefly?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which is better Vector<type> or ArrayList ?

    It has to deal with how modern computer architectures work, and what optimizations a compiler is allowed to make.

    Thread safety only matters if a certain resource can be concurrently accessed from multiple threads. These are shared resources, and a thread-safe program must make sure that the resource is always valid regardless of if it is accessed from one thread or multiple threads. How it does this is beyond the scope here.

    For example, consider a simple bank account resource.

    A thread could request funds to be transferred from one account to another if and only if the account has enough funds to transfer.

    For example, say account 1 has $150.

    What would happen if two threads request $100 dollars to be transferred at the same time?

    One possible execution path is:

    Thread A reads the current value ($150), and decide it is safe to transfer.
    Thread B reads the current value ($150), and decides it is safe to transfer.
    Thread A subtracts $100 from account 1. account 1 now has $50.
    Thread B subtracts $100 from account 1. account 1 now has -$50.

    This is just one possible problem which can happen when shared resources aren't properly synchronized. However, synchronizing isn't free. It can be very expensive to force synchronization on every operation (which is basically what Vector does), and doesn't guarantee that a program is synchronized correctly at all.

    For example, how would you safely remove every other element in a thread-safe manner? Synchronizing on each individual remove operation doesn't guarantee another thread won't muck with the Vector, changing what elements are removed. On the other-hand, it is also too much synchronization. This operation requires a thread to acquire exclusive access to the vector. At this point the thread can use the much cheaper non-thread-safe remove element because it is guaranteed that no other thread can touch the vector until your thread is done modifying it and releases the lock.

    Hence why I don't recommend using Vector ever.

  6. #6
    Junior Member datathinker's Avatar
    Join Date
    Jul 2013
    Location
    Beijing, China
    Posts
    7
    My Mood
    Daring
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Which is better Vector<type> or ArrayList ?

    From my point, they are very similar in funtion but Vectors are a little bit better. First, Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList is unsynchronized, it means it is not thread safe. Second, as data growth is concerned, when the data content is full, Vector will expand its size to a doubled size, however ArrayList only expand by 50%. My reply is general but hopefully it will help at some points.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which is better Vector<type> or ArrayList ?

    @datathinker read my reply on why not use Vector. It covers why internal synchronization in Vector is a very bad thing. Just because you're using a Vector doesn't mean your program is "thread safe".

    An implementation also isn't required to expand an ArrayList by 50% when needed, it is only required to be amortized O(1). I believe there are similar specifications for Vector, but I couldn't find any supporting documentation.

    Regardless of whether expanding the capacity by 50% or 100% they are both amortized O(1) and will perform roughly the same. If you know before-hand you will be inserting a large number of elements, pre-allocate a large enough capacity. This will beat all auto-expand strategy in both memory and time complexity.

  8. #8
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Which is better Vector<type> or ArrayList ?

    hello
    even vector is thread-safe but what about if it is called from non synchronized method? this is why vectors don't guarantee concurrent operations to be safe. because threads still have access to the shared resource without being queued to one at a time. so from my point of view arraylist beats vectors in speed which suits the 21st century

  9. #9
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Which is better Vector<type> or ArrayList ?

    I used to think as datathinker. But I guess I'll definitely have to learn ArrayList. Thanks for all the answers.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which is better Vector<type> or ArrayList ?

    @Alaa That doesn't affect Vector's thread safety. Vector is internally synchronized, so single operations are Thread-safe. However, the issue is there are no transactional operations like I described above with removing every other element.

    // in Thread A
    my_vec.add(new Object());
     
    // in Thread B
    my_vec.add(new Object());

    This is perfectly thread safe, even if there is no wrapping synchronized block or any other synchronization methods.

    // in Thread A
    // remove last element
    my_vec.remove(my_vec.size() - 1);
     
    // in Thread B
    my_vec.remove(0);

    This is not synchronized correctly! The problem is there are a total of 2 operations required by Thread A to be done as a single transaction.

    Suppose my_vec.size() initially is 5.

    Thread A calls my_vec.size(), retrieves 5.
    Thread B decides it wants to remove the first element. This is done synchronously as a single step, so the actual size of my_vec is now 4.
    Thread A calls my_vec.remove(4). Kaboom! You get an out of range exception because there is no 4th element. The last element is actually at index 3 now.

    Fixing this code:

    // in Thread A
    synchronized(my_vec)
    {
        my_vec.remove(my_vec.size() - 1);
    }
     
    // in Thread B
    synchronized(my_vec)
    {
        my_vec.remove(0);
    }

    This is perfectly fine, until you realize that Thread A had to acquire 3 locks just to remove the last element. Additionally, every other Thread must now at a minimum acquire 2 locks to do any operation. Technically if Vector internally uses synchronized(this) you could get away without the synchronized block in thread B, but as far as I can tell there's no guarantees on what type of locking Vector provides so this is a very bad thing to rely on. This is an example of too much synchronization.

    @AndrewR

    Luckily for you, there's not much learning involved. The two classes implement the same interfaces so you can call the same methods and expect basically the same results. Simply declare your variables as ArrayList and you're good

  11. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    copeg (August 14th, 2013), jps (August 15th, 2013)

Similar Threads

  1. How to difine type each object is in a ArrayList
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 23rd, 2013, 08:24 AM
  2. [SOLVED] ArrayList.get() Returning "Incompatible Type"
    By Destro in forum Collections and Generics
    Replies: 2
    Last Post: June 15th, 2012, 06:13 PM
  3. Waht is differenc in Vector and Arraylist
    By diyaots in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 23rd, 2011, 02:25 AM
  4. Difference between Vector and Arraylist
    By Lil_Aziz1 in forum Collections and Generics
    Replies: 4
    Last Post: January 3rd, 2010, 11:03 AM
  5. Difference between Arraylist and Vector in abstractTableModel ?
    By riddhik84 in forum Collections and Generics
    Replies: 2
    Last Post: November 7th, 2009, 04:22 PM