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 5 of 5

Thread: Difference between Vector and Arraylist

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference between Vector and Arraylist

    I know how an Arraylist works but I see a Vector being used more often. What's the difference between them two? On java.sun.com, it said that Arraylist is unsynchronized and Vector is synchronized. Synchronize means to do at same time; thus, making no sense.

    Any help is appreciated.


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between Vector and Arraylist

    I'm sorry. I just realized there's a forum for ArrayList, arrays, and vector.

  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: Difference between Vector and Arraylist

    Synchronization only comes in play when you have a multi-threaded program. Think of it this way:

    Thread A and Thread B both have access to a ArrayList C. What happens if A tries to add something while b is trying to read the current value?

    Ex:
    //C = {1,2,3} (example ArrayList)
    // in thread A:
    System.out.println(C.get(0));
    ...
     
    // somewhere else, in thread B:
    C.set(0,4);

    Would a 1, 4, or neither get printed out? It's impossible to tell since neither the get or set methods are "atomic" (i.e. don't take the absolute minimum number of computational steps to complete). With synchronization, you can at least ensure that the neither category never occurs (this category involves corrupted data, never a good idea). If you design your multi-threaded program well, you can almost completely ensure that which ever one you want to occur will happen (either a 1 or a 4)*

    *note: This last sentence is BS on my part, I think this is possible, but I haven't jumped too deeply into multi-threading in Java yet

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Difference between Vector and Arraylist

    Hell can break loose if you have several threads accessing/altering the same list with no protection/synchronization. Because Vector is synchronized, it safeguards for multithreading. ArrayList is not synchronized (see Helloworld's example). Since synchronization may effect speed, ArrayList is considered faster than Vector (never tested the difference myself, and I bet in many situations of small lists and well written algorithms the difference would be negligeable). Vector pre-dates the Collection framework (and ArrayList) and was incorporated into it.

    I have read (don't recall where though so take this info with a grain of salt) that at that time the Vector class was incorporated and not deprecated due to the dependency of so much software upon it.

    I prefer to use ArrayLists, and if it needs to be synchronized, rather than use Vector will use the Collections framework to synchronize the list (using Collections. synchronizedList()) or synchronize the calls to the changes specifically as opposed to using Vectors. YMMV
    Last edited by copeg; January 2nd, 2010 at 10:59 AM.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Difference between Vector and Arraylist

    The Vector class is old and slow in comparison to the ArrayList. When using lists you can use the Collections static method to create a concurrent list which will be "semi" thread safe

    You should of course synchronize on the list when altering it if you plan on using it in a multithreaded environment

    // Json

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Difference between Arraylist and Vector in abstractTableModel ?
    By riddhik84 in forum Collections and Generics
    Replies: 2
    Last Post: November 7th, 2009, 04:22 PM
  3. byte[] from vector
    By perlWhite in forum Collections and Generics
    Replies: 1
    Last Post: August 26th, 2009, 05:10 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM

Tags for this Thread