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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:41:29  来源:igfitidea点击:

java.util.Vector - alternatives

javavectorarraylist

提问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 Vectorshouldn't be used anymore, and what is the alternative?

Vector不应该再使用的更改会是什么,替代方法是什么?

采纳答案by Paul Wagland

You should use ArrayListinstead of Vector. Vectorused 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

ArrayListis now the better class to use. Vectoris now considered Legacy, and has the added performance overhead of being Thread-Safe.

ArrayList现在是更好用的类。Vector现在被认为是 Legacy,并且增加了线程安全的性能开销。

回答by hexium

You can use an ArrayListinstead.

您可以使用 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 ArrayListwhen you need a Listimplementation but don't need thread safety, and use CopyOnWriteArrayListwhen you need a Listimplementation that is thread safe.

使用ArrayList时,你需要一个List实现,但并不需要线程安全,并且使用CopyOnWriteArrayList时您需要List是线程安全的实现。

回答by Jesper

Vectoris 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 ArrayListand HashMap, which were intended to replace the legacy classes Vectorand Hashtable.

Vector是 Java 1.0 中遗留的集合类。在 Java 1.2(很久以前!)中,添加了集合框架,其中包括新的集合类,例如ArrayListHashMap,旨在替换遗留类VectorHashtable

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 Vectoror Hashtableclasses, but a better way is to use a synchronization wrapper with for example an ArrayList:

在某些情况下(当您的程序是多线程的,并且多个线程访问相同的数据时)您需要同步您的集合。然后有些人会使用旧的VectorHashtable类,但更好的方法是使用同步包装器,例如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 文档。