Results 1 to 11 of 11
- 01-01-2010, 11:16 PM #1
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. ;)
- 01-01-2010, 11:32 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Look up synchronization - the documentation isn't wrong... think of Threads.
- 01-02-2010, 03:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-02-2010, 03:58 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 01-02-2010, 04:01 AM #5
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. :)
- 01-02-2010, 03:53 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-03-2010, 05:45 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 37
- Rep Power
- 0
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.
- 01-03-2010, 10:28 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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 02:50 AM. Reason: Remove code tags
- 01-04-2010, 02:52 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you read the Java API about this?
- 09-04-2012, 08:47 PM #10
Member
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Hi Eranga
great.......
i was puzzled in this topic,
U helped me a lot.
Thanks ma
- 09-04-2012, 09:18 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
Difference between Arraylist and Vector in abstractTableModel ?
By riddhik84 in forum New To JavaReplies: 3Last Post: 11-07-2009, 05:25 PM -
Vector<Point2D> list = new Vector<Point2D>();Is it possible for 15,000 Point2D obj???
By Mazharul in forum Java 2DReplies: 1Last Post: 04-06-2009, 06:45 AM -
what is the difference between jsp and jsf?
By makpandian in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-31-2008, 05:55 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


2Likes
LinkBack URL
About LinkBacks


Bookmarks