Java 6 SE 已经过时了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5763838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 6 SE are vectors obsolete?
提问by Aaron
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..
我使用的是 net beans IDE 6.9.1,它告诉我这是一个“过时的集合”。除了显而易见的那个它已经过时了,我还会在 Java 中使用它吗?我对使用它们感到非常兴奋..
回答by MAK
Use of Vector
s have been discouraged for some time now. They are replaced by ArrayList
, which has more or less the same functionality (but isn't synchronised).
Vector
一段时间以来,不鼓励使用s。它们被替换为ArrayList
,它或多或少具有相同的功能(但不同步)。
回答by Joseph Ottinger
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.
它通常已经过时了,是的。就它是什么而言,它与一个 ArrayList 相同,只是增长特性略有不同;Vector 在每个方法上也是同步的,这意味着每次调用同步都会有(可能的)性能损失。在 Java 6 上,同步远没有以前版本的 Java 那样昂贵,但结果仍然是 ArrayList 而不是 Vector 应该有强烈的偏好。
Similarly, you should prefer HashMap to HashTable.
同样,您应该更喜欢 HashMap 而不是 HashTable。
回答by Yet Another Geek
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.
作为上述答案的说明,您还应该优先使用 ArrayList 的通用版本,因为非通用版本仅用于 Java 1.4(4) 支持。
Example:
例子:
//ArrayList of integers
ArrayList<Integer> list = new ArrayList<Integer>();
回答by Chankey Pathak
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.util.Vector 或 java.util.Hashtable,虽然仍受支持,但这些类已被 JDK1.2 集合类淘汰,可能不应在新开发中使用。