4
package myjava;
import java.util.*;
public class Vectors {
    public static void vec() {
        Vector v = new Vector();
    }
}

I am using net beans IDE 6.9.1 and it tells me it is an "Obsolete Collection". Besides the obvious thats it's obsolete will I ever use it in Java? I was really excited about using them..

Aaron
  • 11,239
  • 18
  • 58
  • 73

4 Answers4

5

Use of Vectors have been discouraged for some time now. They are replaced by ArrayList, which has more or less the same functionality (but isn't synchronised).

MAK
  • 26,140
  • 11
  • 55
  • 86
  • Their deprecation has been informal, not formal. Vector has not been marked as @Deprecated in source. – Joseph Ottinger Apr 23 '11 at 11:19
  • 2
    If therefore a stack is a subclass of a vector have stacks also been deprecated informally of course? – Aaron Apr 23 '11 at 11:22
  • 1
    @Matthew: That's correct. See http://download.oracle.com/javase/6/docs/api/java/util/Stack.html - no deprecation tag, although there's a comment nudging you in the direction of ArrayDeque. – Joseph Ottinger Apr 23 '11 at 11:24
  • 2
    @Matthew: No. I think `Vector`'s use is discouraged because there is an alternate `List` implementation that's part of the collections framework. There are no alternate stack implementations, so `Stack` is still not considered deprecated. – MAK Apr 23 '11 at 11:30
  • @JosephOttinger: Thank you, I've edited my answer to make it clearer. – MAK Apr 23 '11 at 11:32
  • Instead of `Stack` you should better use a `LinkedList` or some other `Deque` implementation, accessing only one end. The `java.util.Stack` class offers much more internal access than useful for a real stack (which should only give `pop`, `push` and maybe `peek` - it should not even be a `Collection` with iterator). – Paŭlo Ebermann Apr 23 '11 at 20:40
2

It's generally obsolete, yes. In terms of what it is, it's the same as an ArrayList with slightly different growth characteristics; Vector is also synchronized on every method, which means there's a (possible) performance penalty on every call for synchronization. On Java 6, the sync is nowhere near as expensive as it was for previous versions of Java, but the upshot is still that there should be a STRONG preference for ArrayList instead of Vector.

Similarly, you should prefer HashMap to HashTable.

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
2

As note to the above answer, you should also preffer using the generic version of ArrayList, because the non-generic version is just for Java 1.4(4) support.

Example:

//ArrayList of integers
ArrayList<Integer> list = new ArrayList<Integer>();
Yet Another Geek
  • 4,251
  • 1
  • 28
  • 40
2

java.util.Vector or java.util.Hashtable, While still supported, these classes were made obsolete by the JDK1.2 collection classes, and should probably not be used in new development.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133