gpt4 book ai didi

如何在Java中删除ArrayList中重复的元素?

转载 作者:知者 更新时间:2024-03-12 12:34:32 24 4
gpt4 key购买 nike

正如我们所知,ArrayList允许添加重复的元素。在这篇文章中,我们写一个程序,用不同的方法从ArrayList中删除重复的元素。

我们可以用三种方法编写逻辑,从ArrayList中删除重复的元素。

  1. 使用HashSet
  2. 使用Java 8 lambda Expression
  3. 使用LinkedHashSet(保持顺序)

1. 使用HashSet

我们知道,HashSet类只包含唯一的值,不允许重复的值加入其中。我们只需将ArrayList转换为HashSet。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: 
" + Arrays.toString(listWithDuplicates.toArray()));
  
         final List<Integer> listWithoutDuplicates = new ArrayList<>(
         new HashSet<>(listWithDuplicates));
     
        System.out.println("After removing duplicates :: "
                                  + Arrays.toString(listWithoutDuplicates.toArray()));
 }
}

输出:

Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [50, 20, 10, 30]

2. 使用Java 8 Lambda表达式

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: "
                                  + Arrays.toString(listWithDuplicates.toArray()));
  
   final List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
        .distinct()
        .collect(Collectors.toList());
     System.out.println("After removing duplicates :: "
                               + Arrays.toString(listWithoutDuplicates.toArray()));
 }
}

输出:

Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [10, 20, 30, 50]
Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [10, 20, 30, 50]

3. 使用LinkedHashSet(保持顺序)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: "
                + Arrays.toString(listWithDuplicates.toArray()));
  
   final List<Integer> listWithoutDuplicates = new ArrayList<>(
         new LinkedHashSet<>(listWithDuplicates));
        System.out.println("After removing duplicates :: " 
               + Arrays.toString(listWithoutDuplicates.toArray()));
     
 }
}

输出

Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [10, 20, 30, 50]

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com