java.util.Vector - 替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792134/
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.util.Vector - alternatives
提问by ST.
Previously I would always have thought a Vector was good to use for non-descript objects when length was unknown. As far as I was aware I thought it was thread-safe too
以前,当长度未知时,我一直认为 Vector 非常适合用于非描述性对象。据我所知,我认为它也是线程安全的
What would change that Vector
shouldn't be used anymore, and what is the alternative?
Vector
不应该再使用的更改会是什么,替代方法是什么?
采纳答案by Paul Wagland
You should use ArrayList
instead of Vector
. Vector
used internal synchronisation, but that is rarely good enough for actual consistency, and only slows down execution when it is not really needed.
您应该使用ArrayList
而不是Vector
. Vector
使用了内部同步,但这对于实际的一致性来说很少足够好,并且只会在真正需要时减慢执行速度。
Also see this stackoverflow question.
另请参阅此 stackoverflow 问题。
回答by Kyle Rozendo
ArrayList
is now the better class to use. Vector
is now considered Legacy, and has the added performance overhead of being Thread-Safe.
ArrayList
现在是更好用的类。Vector
现在被认为是 Legacy,并且增加了线程安全的性能开销。
回答by hexium
You can use an ArrayList
instead.
您可以使用 aArrayList
代替。
If you need a synchronized version, you can do something like:
如果您需要同步版本,您可以执行以下操作:
ArrayList arrayList = new ArrayList();
List synchList = Collections.synchronizedList(arrayList);
回答by Dónal
Use ArrayList
when you need a List
implementation but don't need thread safety, and use CopyOnWriteArrayList
when you need a List
implementation that is thread safe.
使用ArrayList
时,你需要一个List
实现,但并不需要线程安全,并且使用CopyOnWriteArrayList
时您需要List
是线程安全的实现。
回答by Jesper
Vector
is a legacy collection class from Java 1.0. In Java 1.2 (long ago!), the Collections Frameworkwas added which included new collection classes such as ArrayList
and HashMap
, which were intended to replace the legacy classes Vector
and Hashtable
.
Vector
是 Java 1.0 中遗留的集合类。在 Java 1.2(很久以前!)中,添加了集合框架,其中包括新的集合类,例如ArrayList
和HashMap
,旨在替换遗留类Vector
和Hashtable
。
As said before, the legacy collection classes had built-in synchronization, which is unnecessary for many applications. Synchronization has a performance overhead, so if it's not necessary, you shouldn't use it.
如前所述,遗留集合类具有内置同步,这对于许多应用程序来说是不必要的。同步有性能开销,所以如果没有必要,你不应该使用它。
In some cases (when your program is multi-threaded, and multiple threads access the same data) you need to synchronize your collections. Some people would then use the old Vector
or Hashtable
classes, but a better way is to use a synchronization wrapper with for example an ArrayList
:
在某些情况下(当您的程序是多线程的,并且多个线程访问相同的数据时)您需要同步您的集合。然后有些人会使用旧的Vector
或Hashtable
类,但更好的方法是使用同步包装器,例如ArrayList
:
// Your standard, unsynchronized list
List<String> data = new ArrayList<String>();
// Use this to put it into a synchronization wrapper
List<String> syncedData = Collections.synchronizedList(data);
See the API documentation of Collections.synchronizedList()
(and other methods) for more information.
有关Collections.synchronizedList()
更多信息,请参阅(和其他方法)的 API 文档。