Java 6 SE are vectors obsolete?

2019-07-04 11:13发布

问题:

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..

回答1:

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).



回答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.



回答3:

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>();


回答4:

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.



标签: java vector