• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Vector deprecated in 1.3?

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I heard from a colleague that the Vector class was deprecated in Java 1.3? Is that true? What has replaced it?
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How hard is it for you to open Docs and look it up ? it takes a second to do. No it hasn't been depricated and it will not be because for one reason of multithreading. Other wise you should always use ArrayList which is a lot faster then Vector.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, you can use Collections instead of Vectors. However, you can still use Vectors, but then the problem is that, by default Vectors are synchronised & are very slow. So, it's better to use Collections.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector is considered to be a Collection just a type of List.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector was re-worked to implement Collection. If you need synchronization use Vector, otherwise Sun recommends that you use ArrayList.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question of whether Vector has been deprecated in JDK1.3 has already been answered, so I don't need to bring it up again.
Cindy's comment is totally valid, but from my experience I rarely had to make use of a Vector - even in a multi-threaded environment. Here's my justification.
1. Most collections are use to hold static data from a file or database table (e.g. internal cache).
2. The collection is usually populate once - read many.
If both 1 and 2 are true (which I believe is true over 90% of the time), then you should use an ArrayList. I would synchronized the method that is populating the ArrayList. After populating the ArrayList, you can use the Collections.synchronizedList(ArrayList) to get a synchronized list for reading by multiple threads. IMO, this approach is faster than using a Vector.
Just $0.02,
-Peter
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In terms of what they can do. Is "synchronization" the only difference between the Vector and ArrayList?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A big difference apparent from a glance at the API is that Vector has alot more methods than ArrayList...take a look, you'll see.
http://java.sun.com/j2se/1.3/docs/api/index.html

------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
ken chou
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That's right. Vector has a lot more methods than ArrayList. So in terms of manipulating data, using Vector is a lot more handy than ArrayList, right?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. Almost all the "extra" methods in Vector are merely trivial renaming of other methods. Well, actually it's the other methods that are renamings, since the Vector methods came first. Example: Vector has elementAt(int) method; when they built ArrayList they renamed this to get(int), and added get(int) to Vector as well even though it does the exact same thing as elementAt(int). They retained the old method name so as not to break existing code, but for new development it's easier to just use the new add(int) method for both ArrayList and Vector.
Anyway, almost everything in Vector is trivial to accomplish under the ArrayList API. The only exceptions I found were:
ArrayList has no constructor to set capacity increment - good, this is almost always a bad idea; exponential growth is much better.
Methods:
capacity() - umm, so what?
indexOf(Object, int) - use subList + indexOf to mimic
lastIndexOf(Object, int) - use subList + lastIndexOf to mimic
setSize() - probably misunderstood by a lot of people, and not very useful anyway. What's the point of having a growable Collection if you're going to set the size? Might as well use an array in that case.
[This message has been edited by Jim Yingst (edited March 21, 2001).]
 
ken chou
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to Peter and Jim's Analysis. It seems like we shouldn't use Vector any more.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand Cindy's post - If you need your collection to be thread-safe or you don't have access to the Java 2 Collections, then use Vector, otherwise use ArrayList.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
To expand Cindy's post - If you need your collection to be thread-safe or you don't have access to the Java 2 Collections, then use Vector, otherwise use ArrayList.


Mmmm...
If you need your collection to be threadsafe, think twice before using Vector. I've seen too many greenhorns end up on the wrong side of an angry bull because they thought they were threadsafe by using a Vector.
The fact that a Vector is synchronized means that multiple threads won't make a mess of your Vector. No more, no less. It does not mean that the logic you're building around the Vector is threadsafe. In fact, it usually isn't, but the Vector will lure you into thinking you're OK. And when you've finally debugged your code you will have ended up unnecessarily acquiring monitor locks twice, once in your class, once in the Vector.
My recommendation would be to always use an ArrayList instead, and to carefully synchronize those actions that need synchronizing; no more, no less.
- Peter
 
Onion rings are vegetable donuts. Taste this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic