• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and Vector

 
Tulsi Rai
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Tulsi Rai
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new = as of Java 1.2 where Collections entered the scene...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic