aspose file tools*
The moose likes Java in General and the fly likes In what cases we would generally use a Vector  class?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "In what cases we would generally use a Vector  class??" Watch "In what cases we would generally use a Vector  class??" New topic
Author

In what cases we would generally use a Vector class??

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi,

theoritcally it mentions that we should use a Vector when we are bothered about Thread Safety , but generally each Thread will have its own copy of data so typically while fetching data from database in APplications we generally store it in a ArrayList , which is very common .

Could anybody tell me a scenario where we should compulsarily use a vector only .

Waiting for your responses . Thanks .


Save India From Corruption - Anna Hazare.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Personally, I would say you should never use the Vector. There are multiple reasons why, and if you care to find them you can search. But for the sake of your question there are replacements, such as a synchronized list returned from Collections.synchronizedList(). So instead of reading your question as 'When should we use a Vector?' I will answer it as 'When should we use a Synchronized List?'

You use the synchronized list when you share the SAME List instance across multiple threads. If you have one List per Thread, then use ArrayList. But if you have a single pool of data which multiple Threads need to access, then you need to synchronize access to the data, and so a Synchronized List gets you part way there (you still need to do some external synchronization when doing multiple-access operations like swapping values, iterating, or checking if a value is there-then getting it if it is).


Steve
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ravi Kiran Va wrote:generally each Thread will have its own copy of data

No. Local methods will, but what about static variables ? Or instance variables whose instance is shared between several threads ?


[My Blog]
All roads lead to JavaRanch
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

So far I've only used Vectors in versions of the JVM that don't have Lists available, for instance, while working with SunSPOT.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
There is at least one class which takes Vector as a constructor parameter. I think it might be JList or JComboBox. I would have preferred they take List<T> as a parameter.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Campbell Ritchie wrote:There is at least one class which takes Vector as a constructor parameter. I think it might be JList or JComboBox. I would have preferred they take List<T> as a parameter.


Unfortunately, both of them do. This or Object[].


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

As does DefaultTableModel (and JTable through it).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Luke for nice explanation.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or you can use a CopyOnWriteArrayList in case of a write-less-read-many situation.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
jQuery in Action, 2nd edition
 
subject: In what cases we would generally use a Vector class??