File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList and Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList and Vector" Watch "ArrayList and Vector" New topic
Author

ArrayList and Vector

Tulsi Rai
Ranch Hand

Joined: Jun 26, 2002
Posts: 55
Hi Folks,
I know that ArrayList is essentially an unsynchronized Vector. And Since Vector is synchronized, it is slower than ArrayList. When synchronization is needed, Vector is chosen over ArrayList. Now, could you please tell me, ok these are the situations where Vector must be used instead of ArrayList.
Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Vector should never be used in new code, except if you're using an API or library method that requires you to pass a Vector as a method argument. If you need a synchronized List implementation, you can get one from the method Collections.synchronizedList().


[Jess in Action][AskingGoodQuestions]
Tulsi Rai
Ranch Hand

Joined: Jun 26, 2002
Posts: 55
Hi Ernest,
Thanks for your help.
Originally posted by Ernest Friedman-Hill:
Vector should never be used in new code, except if you're using an API or library method that requires you to pass a Vector as a method argument. If you need a synchronized List implementation, you can get one from the method Collections.synchronizedList().


Could you please explain me what do you mean by "new code". Could you tell me your real world experience where you had no choice other than use a synchronized List. I am trying to see what would be the situation like where we must use synchronized list.
thanks again.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Re the "not in new code" idea ... Vector and HashTable were very early Java classes that Sun quickly decided to replace with better ones. They just weren't very well designed from the API and naming point of view. Unfortunately everybody learns Vector and HashTable in introductory books and tutorials and keeps on using them. Learn to love the Collection classes like ArrayList and HashMap. You will just be that much cooler than the people who keep writing Vectors.

Here's a Collections Crib Sheet I copied from a Sun newsletter cause I can't keep all the collections classes in my head.
[ July 02, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Actually "old code" can be also recognized from the name clue Hashtable, the so-called 'new' code would have used 'HashTable', like in HashMap.

old new
-------- ---------
Hashtable ==> HashMap
Vector ==> ArrayList

the new Java 1.2 code uses 'em..
ciao,
R�stem.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
To answer your underlying question, you normally synchronize a method when it can be invoked by two or more threads at the same time. This is because your data can be corrupted if two threads are updating the same varables and objects at the same time.

The example usually given is deducting a cash withdrawal from your account balance (balance = balance - debit ; ) . Let's say you started with $100 and both you and you wife, at different ATM's, take out $20.
The first thread retrieves balance, $100.
The second thread retrieves balance, $100.
The first thread subtracts $20, leaving $80.
The first thread stores $80 in balance.
The second thread subtracts $20 from its copy of balance, leaving $80.
The second thread stores $80 in balance.

You and your wife are happy, but the bank is out $20.

The same thing can happen to the indexes in an ArrayList if two threads add an element at the same time. It gets even worse if the ArrayList is full and both threads make separate new copies of the private array Object[] inside your ArrayList.

Actually, Vector is now just a version of ArrayList with synchronized added, but it is unfashionable to use obsolescent classes when new ones are available.

By the way, real experts synchronize just the part of their code that is vulnerable, but that is tricky.
[ January 13, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tulsi Rai:
Hi Ernest,
Thanks for your help.


Could you please explain me what do you mean by "new code".

I think "new code" means code that you are writing right now. Where as "old code" means code that you (or someone else) wrote yesterday, last week, last year, or ten years ago.


Java API Documentation
The Java Tutorial
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
new = as of Java 1.2 where Collections entered the scene...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList and Vector