Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Eranga
  • 1 Post By JosAH

Thread: Difference between Vector and Arraylist

  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    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
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Look up synchronization - the documentation isn't wrong... think of Threads.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Synchronize means to do at same time; thus, making no sense.
    No it make sense a lot.

    Vectors are synchronized, means any method that deals vector's contents is thread safe. But ArrayList is not synchronized, not thread safe then. With that difference, synchronization will introduce a performance hit into your application/code. So if you don't need a thread-safe collection, use the ArrayList. Dealing with thread essential in most of the industrial level application.
    AnanthFrancis likes this.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    When they say Vector is synchronized they mean that is safe to use when there are multiple threads (as others have said) not that the class itself uses multiple threads. It's easy to how your confusion might arise.

    If you want a synchronized list use wrapper around some other implementation like an ArrayList or LinkedList. Vector is very 20th century.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Well, i know nothing about Threads yet. Going to read java.sun.com/docs/books/tutorial/essential/concurrency/ and hopefully synchronization in java makes sense.
    Btw, I love your profile pic Eranga. :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Well, i know nothing about Threads yet.
    Then it's better to study more about Threads first of all my friend.

    Quote Originally Posted by Lil_Aziz1 View Post
    Btw, I love your profile pic Eranga. :)
    If you loved my comments, it's really nice lol. I just want to let what I know. ;)

  7. #7
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    Vector and ArrayList are very similar. Both of them represent a 'growable array', where you access to the elements in it through an index.

    ArrayList it's part of the Java Collection Framework, and has been added with version 1.2, while Vector it's an object that is present since the first version of the JDK. Vector, anyway, has been retrofitted to implement the List interface.

    The main difference is that Vector it's a synchronized object, while ArrayList it's not.

    While the iterator that are returned by both classes are fail-fast (they cleanly thrown a ConcurrentModificationException when the orignal object has been modified), the Enumeration returned by Vector are not.

    Unless you have strong reason to use a Vector, the suggestion is to use the ArrayList.
    RAQ Report: free Java reporting tool.

  8. #8
    OutOfMemory is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    As everybody has already explained ..ArrayList is a new unsynchronized form of an earlier indexed and ordered data structure Vector, which is there in jdk since inception (although later changed to incorporate list interface)...

    So vector should not be used in a single threaded environment.As sun has already said vectors are slower than arraylist.

    Even in threaded environment instead of using vector ,one can get a synchronized list by calling Collections.synchronizedList(list) which returns a thread safe version of list.

    I am not sure but somewhere I have read that Collections.synchronizedList(list) method was buggy in jdk1.4 .. can anybody tell what it is ?
    Last edited by Eranga; 01-04-2010 at 03:50 AM. Reason: Remove code tags

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  10. #10
    AnanthFrancis is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Hi Eranga

    great.......
    i was puzzled in this topic,
    U helped me a lot.

    Thanks ma

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between Vector and Arraylist

    I'm closing this thread before more insane replies are posted here ...

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:17 PM
  2. Replies: 3
    Last Post: 11-07-2009, 06:25 PM
  3. Replies: 1
    Last Post: 04-06-2009, 07:45 AM
  4. what is the difference between jsp and jsf?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-31-2008, 06:55 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •