• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Vector over String  RSS feed

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

When I retreive values from database, I store it in string variables.

Is there any advantage in storing in vector? Will it improve the perfomance of my java code?

Thanks
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Vector is a Collection List where you can add n number of items(may be strings) to a single Vector Object and use that object as a reference to all your added Strings.

There are lot about Collections, Please go through the Collections on Java.

Ashok
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector is also very very slow compared to other Collections.
It's a legacy class that should generally NOT be used.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeroen is correct. Vector is only still there to support old stuff, but new code should avoid it.

It is important to know, also, that Vector is single-threaded (i.e., synchronized), and that Vector implements List.

It is far better to declare your variable references of type List and the concrete implementation of some newer type. e.g:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jeroen]: Vector is also very very slow compared to other Collections.

Not in my experience. It's very very pointless to use for any reason other than compatibility with some archaic API, but the performance is pretty much comparable to ArrayList. The synchronization issue makes remarkably little difference if you're not actually using multiple threads.

[Matt]: Vector is single-threaded (i.e., synchronized)

Vector is synchronized, sure. (Rather pointlessly I may add.) But since when does that mean "single-threaded"? To me a single-threaded application only has one thread, period, while a synchronized class may allow many different threads to access it - but only one at a time. (Assuming the most common scenario, all methods synchronized on the same monitor.) To me those are very different situations, and I wouldn't want to see them conflated. I would say Vector is synchronized, but not single-threaded. (Unless you use it in a single-threaded application.)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of strings if you use Vectors ,they automatically expand themselves. If you want to display the values dynamically ,you can use vectors.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, I've timed Vector against ArrayList and found that the same method processing the same dataset (several thousand records) was an order of magnitude faster when using ArrayList than it was using Vector (milliseconds rather than seconds).

I'd hardly call that an insignificant difference.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeroen T Wenting:
Jim, I've timed Vector against ArrayList and found that the same method processing the same dataset (several thousand records) was an order of magnitude faster when using ArrayList than it was using Vector (milliseconds rather than seconds).

I'd hardly call that an insignificant difference.



that depends on a lot of things, including your operating system, which version of the JVM you're running, etc.

Not that I disagree with you. Array list is faster because it doesn't have the overhead of synchronization. However, the point that you are arguing on is one that varies from machine to machine, so while I'm not saying you are wrong, I also feel that Jim is just as correct. It's certainly not so horrendously slow that it would turn a complicated algorithm performed on each element of a list into an massively slow operation. If the algorithm is even slightly complex, the time to perform the computations on each element of the list is so much greater than the access time that the synchronization overhead is essentially negligible. If it takes 200 times as long to perform the calculations as it does to get the access the element of the list, then who cares about a magnitude difference in the access time? You've just increased total run time by, what, 5%, maybe? Is it really that important? Sometimes, yes, sometimes, no.

Furthermore, in my experience, in many (but admittedly not all) cases where you are really concerned about speed of accessing data in a collection, you will want to be using the fastest jvm available, in which case, you don't really have to worry about the synchronization cost (for example, in 1.5, it can almost be ignored in most cases). Also, for situations where you ARE using multithreading, you would have to synchronize your array list as well, so there, the point is basically moot.

Anyway, my vote is for ArrayList, but I wouldn't say that vector is EXTREMELY slower that ArrayList in all, or even most, situations.

- Adam
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by preeti math:
Instead of strings if you use Vectors ,they automatically expand themselves. If you want to display the values dynamically ,you can use vectors.



See, to me, you're comparing apples and oranges. Sure, a Vector could be used to store Character objects, but really, if you're looking for an expandable character array class, wouldn't StringBuffer of StringBuilder be more appropriate (not to mention, more optimized to that specific task)?

- Adam
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the OP Cosmos comes back, I'm curious if you're storing a row at a time in a collection (vector or whatever) or multiple columns or something else?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!