Java ArrayList vs Vector – Java 阵列列表与矢量

最后修改: 2019年 1月 8日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to focus on the differences between the ArrayList and Vector classes. They both belong to the Java Collections Framework and implement the java.util.List interface.

在本教程中,我们将重点讨论ArrayListVector类之间的区别。它们都属于Java集合框架,并实现了java.util.List接口。

However, these classes have significant differences in their implementations.

然而,这些类在实现上有很大的不同

2. What’s Different?

2.有什么不同?

As a quick start, let’s present the key differences of ArrayList and Vector. Then, we’ll discuss some of the points in more detail:

作为一个快速的开始,让我们介绍一下ArrayListVector.的主要区别。然后,我们将更详细地讨论其中的一些要点。

  • synchronization – The first major difference between these two. Vector is synchronized and ArrayList isn’t.
  • size growth – Another difference between the two is the way they resize while reaching their capacity. The Vector doubles its size. In contrast, ArrayList increases only by half of its length
  • iteration – And Vector can use Iterator and Enumeration to traverse over the elements. On the other hand, ArrayList can only use Iterator.
  • performance – Largely due to synchronization, Vector operations are slower when compared to ArrayList
  • framework – Also, ArrayList is a part of the Collections framework and was introduced in JDK 1.2. Meanwhile, Vector is present in the earlier versions of Java as a legacy class.

3. Vector

3.矢量

As we already have an extended guide about ArrayList, we won’t discuss its API and capabilities here. On the other hand, we’ll present some core details about Vector.

由于我们已经有了关于ArrayList的扩展指南,我们不会在这里讨论其API和功能。另一方面,我们将介绍一些关于Vector的核心细节。

Simply put, a Vector is a resizable array. It can grow and shrink as we add or remove the elements.

简单地说一个矢量是一个可调整大小的数组。当我们添加或删除元素时,它可以增长和缩小。

We can create a vector in typical fashion:

我们可以用典型的方式创建一个矢量。

Vector<String> vector = new Vector<>();

The default constructor creates an empty Vector with an initial capacity of 10.

默认构造函数创建一个空的Vector,初始容量为10。

Let’s add a few values:

让我们添加一些数值。

vector.add("baeldung");
vector.add("Vector");
vector.add("example");

And finally, let’s iterate through the values by using the Iterator interface:

最后,让我们通过使用Iterator接口来迭代这些值。

Iterator<String> iterator = vector.iterator();
while (iterator.hasNext()) {
    String element = iterator.next();
    // ...
}

Or, we can traverse the Vector using Enumeration:

或者,我们可以使用Enumeration来遍历Vector

Enumeration e = vector.elements();
while(e.hasMoreElements()) {
    String element = e.nextElement();
    // ... 
}

Now, let’s explore some of their unique features in more depth.

现在,让我们更深入地探讨它们的一些独特功能。

4. Concurrency

4.并发性

We’ve already mentioned that ArrayList and Vector are different in their concurrency strategy, but let’s take a closer look. If we were to dive into Vector’s method signatures, we’d see that each has the synchronized keyword:

我们已经提到ArrayListVector的并发策略不同,但让我们仔细看看。如果我们深入研究Vector的方法签名,我们会发现每个方法都有synchronized关键字。

public synchronized E get(int index)

Simply put, this means that only one thread can access a given vector at a time.

简单地说,这意味着一次只有一个线程可以访问一个给定的向量

Really, though, this operation-level synchronizations needs to be overlayed anyway with our own synchronization for compound operations.

实际上,无论如何,这种操作层面的同步需要与我们自己的复合操作的同步叠加在一起。

So in contrast, ArrayList takes a different approach. Its methods are not synchronized and that concern is separated out into classes that are devoted to concurrency.

所以相比之下,ArrayList采取了不同的方法。它的方法是同步的,而且这种关注被分离出来,成为专门用于并发的类。

For example, we can use CopyOnWriteArrayList or Collections.synchronizedList to get a similar effect to Vector:

例如,我们可以使用CopyOnWriteArrayList 或者Collections.synchronizedList来获得与Vector类似的效果。

vector.get(1); // synchronized
Collections.synchronizedList(arrayList).get(1); // also synchronized

5. Performance

5.表现

As we already discussed above, Vector is synchronized which causes a direct impact on performance.

正如我们在上面已经讨论过的,向量是同步的,这对性能造成了直接影响

To see the performance difference between Vector versus ArrayList operations, let’s write a simple JMH benchmark test.

为了了解VectorArrayList操作之间的性能差异,让我们编写一个简单的JMH基准测试。

In the past, we’ve looked at the time complexity of ArrayList‘s operations, so let’s add the test cases for Vector.

在过去,我们已经研究了ArrayList操作的时间复杂度,所以让我们添加Vector的测试案例。向量

First, let’s test the get() method:

首先,让我们测试一下get()方法。

@Benchmark
public Employee testGet(ArrayListBenchmark.MyState state) {
    return state.employeeList.get(state.employeeIndex);
}

@Benchmark
public Employee testVectorGet(ArrayListBenchmark.MyState state) {
    return state.employeeVector.get(state.employeeIndex);
}

We’ll configure JMH to use three threads and 10 warmup iterations.

我们将把JMH配置为使用三个线程和10次预热迭代。

And, let’s report on the average time per operation at the nanosecond level:

而且,让我们报告一下纳秒级的每个操作的平均时间。

Benchmark                         Mode  Cnt   Score   Error  Units
ArrayListBenchmark.testGet        avgt   20   9.786 ± 1.358  ns/op
ArrayListBenchmark.testVectorGet  avgt   20  37.074 ± 3.469  ns/op

We can see that ArrayList#get works about three times faster than Vector#get.

我们可以看到,ArrayList#get的工作速度是Vector#get的三倍左右。

Now, let’s compare the results of the contains() operation:

现在,让我们比较一下contains()操作的结果。

@Benchmark
public boolean testContains(ArrayListBenchmark.MyState state) {
    return state.employeeList.contains(state.employee);
}

@Benchmark
public boolean testContainsVector(ArrayListBenchmark.MyState state) {
    return state.employeeVector.contains(state.employee);
}

And print the results out:

并将结果打印出来。

Benchmark                              Mode  Cnt  Score   Error  Units
ArrayListBenchmark.testContains        avgt   20  8.665 ± 1.159  ns/op
ArrayListBenchmark.testContainsVector  avgt   20  36.513 ± 1.266  ns/op

As we can see, for the contains() operation, the performance time for Vector is much longer than ArrayList.

我们可以看到,对于contains()操作,Vector的性能时间比ArrayList长很多。

6. Summary

6.总结

In this article, we had a look at the differences between the Vector and ArrayList classes in Java. Additionally, we also presented Vector features in more details.

在这篇文章中,我们了解了Java中VectorArrayList类之间的区别。此外,我们还更详细地介绍了Vector的特性。

As usual, the complete code for this article is available over on GitHub.

像往常一样,本文的完整代码可以在GitHub上找到