• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
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 .
 
Bartender
Posts: 4179
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).
 
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 ?
 
Saloon Keeper
Posts: 9378
181
  • 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.
 
Marshal
Posts: 60806
190
  • 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.
 
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[].
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Bartender
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!