This is a discussion on Which is best Vector or ArrayList? within the Java Programming forums, part of the Software Development category; Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one
__________________ cheers Aman |
| |||
| I have seen many posts warning people to staty away from Vector and use ArrayList since ArrayList is "much faster". I have done time tests (using 1.3) and found no real difference. On occasion, Vector was even faster. If you look at its implementation, Vector is backed by an array which makes access quick. Since Vector is what is also used by older existing classes and it is also a List, I think that Vector is a better choice. Does anyone have any specific evidence that ArrayList is so much better than Vector? |
| |||
| Hi, Vector has another benefit too - it is a thread save class - you can add something to the vector or remove something and must not care about other threads, that have access to the vector at the same time. A Vector is easy to use and, I guess, it is used by DefaultTableModel (model from JTable), because it is thread save - you can access the model by one thread without keeping track of concurrently access by the event handling thread for example. So if you have the choice, choose Vector - if the difference in execution time is relevant for your application, you should better look out for a better algorithm, because the difference is only marginal and you will not win much time by using ArrayList instead of Vector.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| The difference between the two is that a Vector is synchronized and an ArrayList is not. Since the Vector is synchronized it has overhead for each call to the Vector. The Vector may appear to be eaqually as fast in a simple test, but it is in fact slower.
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| Since my time tests couldn't find any difference, does anyone have specific results of tests which show how much slower Vector actually is than ArrayList? It would be good if I knew what the difference was so I knew how much weight to give the time in design considerations. Is the difference just fractions of a percent? . I was testing enough iterations that I think Iwould have found differences if they were several percent. While I do believe that being synchronized has some overhead, it may be a trivial amount. Some people seem emphatic that Vector is bad and ArrayList is good, but without any specifics. |
| |||
| BruceEckel.com has some performance numbers I believe regarding synchronization. I think I read that synchronized calls can take up to 4 times as long. Don't forget also the synchronization affects Enumeration vs Iterator and other helper classes to the Vector. While ArrayList can be faster, it is probably not enough in most applications. We ran some performance figures once and we needed hundreds of thousands of entries before the difference became appreciable. I always use ArrayList but it's just preference the synchronization isn't always good. ArrayList also had many other new methods that are from the Map family which are nice to have. But if you like Vectors use them. You probably heard the first rule of performance tuning is "don't do it"; i.e. don't spend time trying to make something faster until you know where your bottlenecks are. Esp. don't rewrite a bunch of old code to use Vectors it will not be worth it. |
| |||
| The perfemance time for synchronization seems like just what I need. Where in the Bruce Eckel site would I need to go to find that information. Is it free ot do I need to buy one of their books/seminars? |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| "Cannot Find Symbol" when I trying Vector to ArrayList | amansundar | Java Programming | 2 | 11-28-2007 02:18 AM |
| Type conversion from arraylist to string in C# | oxygen | C# Programming | 3 | 07-30-2007 09:47 AM |
| Difference between Array and ArrayList | leoraja8 | Java Programming | 1 | 05-11-2007 04:06 AM |
| Arraylist | leoraja8 | Java Programming | 1 | 05-11-2007 03:47 AM |
| Convert Object to Arraylist and Arraylist to Object | raja | C# Programming | 0 | 05-08-2007 03:26 AM |