Differences Between Collection.clear() and Collection.removeAll() – Collection.clear()和Collection.removeAll()的区别

最后修改: 2018年 9月 17日

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

1. Overview

1.概述

In this quick tutorial, we’ll learn about two Collection methods that might seem to do the same thing, but aren’t: clear() and removeAll().

在这个快速教程中,我们将学习两个Collection方法,它们似乎做了同样的事情,但其实不是。clear()removeAll()

We’ll first see the method definitions and then use them in short examples.

我们先看看方法的定义,然后在简短的例子中使用它们。

2. Collection.clear()

2.Collection.clear()

We’ll first dive into the Collection.clear() method. Let’s check the Javadoc of the method. According to it, the purpose of clear() is to remove every single element from the list.

我们将首先深入研究Collection.clear()方法。让我们看看该方法的Javadoc。根据它,clear()的目的是移除列表中的每个元素。

So, basically, calling clear() on any list will result in the list becoming empty.

所以,基本上,在任何列表上调用clear()都会导致列表变空。

3. Collection.removeAll()

3.Collection.removeAll()

We’ll now have a look at the Javadoc of Collection.removeAll(). We can see that the method takes a Collection as an argument. And its purpose is to remove all common elements between the list and the collection.

我们现在来看看Collection.removeAll()Javadoc。我们可以看到,该方法需要一个Collection作为参数。而且它的目的是 删除列表和集合之间的所有共同元素。

So, when calling it on a collection, it will remove all elements from the passed argument that are also in the collection on which we call removeAll().

因此,当在一个集合上调用它时,它将从传递的参数中移除所有也在我们调用removeAll()的集合中的元素。

4. Examples

4.例子

Let’s now look at some code to see those methods in action. We’ll first create a test class with the name ClearVsRemoveAllUnitTest.

现在让我们看看一些代码,看看这些方法的作用。我们首先创建一个测试类,名称为ClearVsRemoveAllUnitTest

After that, we’ll create a first test for Collection.clear().

之后,我们将为Collection.clear()创建第一个测试。

We’ll initialize a collection of Integers with a few numbers and call clear() on it so that no element remains in the list:

我们将用几个数字初始化一个Integers集合,并对其调用clear(),这样列表中就没有元素了。

@Test
void whenClear_thenListBecomesEmpty() {
    Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

    collection.clear();

    assertTrue(collection.isEmpty());
}

As we can see the collection is empty after clear() being called.

我们可以看到,在clear()被调用后,集合是空的。

Let’s create a second test with two collections, one with numbers from 1 to 5 and the other with numbers from 3 to 7. After that, we’ll call removeAll() on the first collection with the second one as a parameter.

让我们用两个集合创建第二个测试,一个是1到5的数字,另一个是3到7的数字。之后,我们将在第一个集合上调用removeAll(),第二个集合作为参数。

We’ll expect only the numbers 1 and 2 to remain in the first collection (while the second one is unchanged):

我们将期望在第一个集合中只保留数字1和2(而第二个集合则没有变化)。

@Test
void whenRemoveAll_thenFirstListMissElementsFromSecondList() {
    Collection<Integer> firstCollection = new ArrayList<>(
      Arrays.asList(1, 2, 3, 4, 5));
    Collection<Integer> secondCollection = new ArrayList<>(
      Arrays.asList(3, 4, 5, 6, 7));

    firstCollection.removeAll(secondCollection);

    assertEquals(
      Arrays.asList(1, 2), 
      firstCollection);
    assertEquals(
      Arrays.asList(3, 4, 5, 6, 7), 
      secondCollection);
}

And our expectations are met. Only the numbers 1 and 2 are remaining in the first collection and the second one hasn’t been changed.

而我们的期望得到了满足。第一个系列中只剩下数字1和2,第二个系列还没有被改变。

5. Conclusion

5.结论

In this article, we’ve seen the purposes of Collection.clear() and Collection.removeAll().

在这篇文章中,我们已经看到了Collection.clear()Collection.removeAll()的目的。

Despite what we might think at first, they aren’t doing the same thing. clear() deletes every element from the collection and removeAll() one only removes the elements matching those from another Collection.

尽管我们一开始可能认为,它们做的不是同一件事。clear()从集合中删除每个元素,而removeAll()只删除与另一个Collection中的元素匹配的元素。

And, as always, the code can be found over on GitHub.

而且,像往常一样,可以在GitHub上找到代码