This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stephan van Hulst
Bartender
Pie
Posts: 6106
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[].
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As does DefaultTableModel (and JTable through it).
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Luke for nice explanation.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use a CopyOnWriteArrayList in case of a write-less-read-many situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic