Removing all duplicates from a List in Java – 在Java中删除列表中的所有重复内容

最后修改: 2014年 6月 12日

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

1. Introduction

1.介绍

In this quick tutorial, we’re going to learn how to clean up the duplicate elements from a List. First, we’ll use plain Java, then Guava, and finally, a Java 8 Lambda-based solution.

在这个快速教程中,我们将学习如何清理列表中的重复元素。首先,我们将使用普通Java,然后是Guava,最后是基于Java 8 Lambda的解决方案。

This tutorial is part of the Java – Back to Basic” series here on Baeldung.

本教程是Baeldung网站上Java – Back to Basic“系列的一部分。

2. Remove Duplicates From a List Using Plain Java

2.使用普通Java删除列表中的重复内容

We can easily remove the duplicate elements from a List with the standard Java Collections Framework through a Set:

我们可以用标准的Java集合框架通过Set轻松地删除List中的重复元素。

public void 
  givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates = new ArrayList<>(
      new HashSet<>(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(5));
    assertThat(listWithoutDuplicates, containsInAnyOrder(5, 0, 3, 1, 2));
}

As we can see, the original list remains unchanged.

我们可以看到,原来的名单仍然没有改变。

In the example above, we used HashSet implementation, which is an unordered collection. As a result, the order of the cleaned-up listWithoutDuplicates might be different than the order of the original listWithDuplicates.

在上面的例子中,我们使用了HashSet实现,它是一个无序的集合。因此,清理后的listWithoutDuplicates的顺序可能与原listWithDuplicates的顺序不同。

If we need to preserve the order, we can use LinkedHashSet instead:

如果我们需要保留顺序,我们可以使用LinkedHashSet代替。

public void 
  givenListContainsDuplicates_whenRemovingDuplicatesPreservingOrderWithPlainJava_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates = new ArrayList<>(
      new LinkedHashSet<>(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(5));
    assertThat(listWithoutDuplicates, containsInRelativeOrder(5, 0, 3, 1, 2));
}

3. Remove Duplicates From a List Using Guava

3.使用Guava删除列表中的重复内容

We can do the same thing using Guava as well:

我们也可以用Guava做同样的事情。

public void 
  givenListContainsDuplicates_whenRemovingDuplicatesWithGuava_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates 
      = Lists.newArrayList(Sets.newHashSet(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(5));
    assertThat(listWithoutDuplicates, containsInAnyOrder(5, 0, 3, 1, 2));
}

Here also, the original list remains unchanged.

在这里,原来的名单也保持不变。

Again, the order of elements in the cleaned-up list might be random.

同样,清理过的列表中的元素的顺序可能是随机的。

If we use the LinkedHashSet implementation, we’ll preserve the initial order:

如果我们使用LinkedHashSet实现,我们将保留初始顺序。

public void 
  givenListContainsDuplicates_whenRemovingDuplicatesPreservingOrderWithGuava_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates 
      = Lists.newArrayList(Sets.newLinkedHashSet(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(5));
    assertThat(listWithoutDuplicates, containsInRelativeOrder(5, 0, 3, 1, 2));
}

4. Remove Duplicates From a List Using Java 8 Lambdas

4.使用Java 8 Lambdas从列表中删除重复的内容

Finally, let’s look at a new solution, using Lambdas in Java 8. We’ll use the distinct() method from the Stream API, which returns a stream consisting of distinct elements based on the result returned by the equals() method.

最后,让我们看看一个新的解决方案,在Java 8中使用Lambdas。我们将使用Stream API中的distinct()方法,该方法根据equals()方法返回的结果,返回一个由不同元素组成的流。

Additionally, for ordered streams, the selection of distinct elements is stable. This means that for duplicated elements, the element appearing first in the encounter order is preserved:

此外,对于有序的流,不同元素的选择是稳定的。这意味着,对于重复的元素,在相遇顺序中首先出现的元素被保留下来。

public void 
  givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
     .distinct()
     .collect(Collectors.toList());

    assertThat(listWithoutDuplicates, hasSize(5));
    assertThat(listWithoutDuplicates, containsInAnyOrder(5, 0, 3, 1, 2));
}

There we have it, three quick ways to clean up all the duplicate items from a List.

我们有三个快速方法来清理List.中的所有重复项目。

5. Conclusion

5.总结

In this article, we demonstrated how easy it is to remove duplicates from a list using plain Java, Google Guava, and Java 8.

在这篇文章中,我们演示了使用普通Java、Google Guava和Java 8删除列表中的重复内容是多么简单。

The implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project, so it should be easy to import and run.

所有这些例子和片段的实现都可以在GitHub项目中找到。这是一个基于Maven的项目,所以它应该很容易导入和运行。