如果线程安全不是问题,那么 Java 中的 ArrayList 与 Vectors [英] ArrayList vs. Vectors in Java if thread safety isn't a concern

查看:15
本文介绍了如果线程安全不是问题,那么 Java 中的 ArrayList 与 Vectors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VectorArrayList 的性能真的有那么大的不同吗?在不存在线程安全问题的情况下,始终使用 ArrayList 是否是一种好习惯?

Is there really that much of a difference between the performance of Vector and ArrayList? Is it good practice to use ArrayLists at all times when thread safety isn't an issue?

推荐答案

Vector 起源于 Pre-Collections API 时代,并且经过改造成为其中的一部分.据我所知,它没有被弃用的原因是核心 API 依赖于它.

Vector originates back from the pre-Collections API days, and have been retrofitted since to be a part of it. From what I've read, the reason it is not deprecated is because the core API depends on it.

ArrayList 是作为 Collections API 的一部分从头开始编写的,因此除非您需要支持低至 1.2 的 Java 版本,否则应该使用它.

ArrayList was written from scratch as a part of the Collections API and as such should be used unless you need to support Java versions down to 1.2.

如果你需要一个线程安全的 ArrayList,你可以使用静态工厂方法 Collections.synchronizedList(new ArrayList); 来生成你的列表.

If you need a thread-safe ArrayList, you can use the static factory method Collections.synchronizedList(new ArrayList<type>); to generate your list.

这篇关于如果线程安全不是问题,那么 Java 中的 ArrayList 与 Vectors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆