• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15486
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79152
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Luke for nice explanation.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can use a CopyOnWriteArrayList in case of a write-less-read-many situation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic