Java 如何删除ArrayList中的重复值

Java 如何删除ArrayList中的重复值

给定一个有重复值的ArrayList,任务是在Java中从该ArrayList中删除重复的值。

例子。

输入: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5, 5]
输出: List = [1, 10, 2, 3, 4, 5]

输入: List = [“G”, “e”, “e”, “k”, “s”]
输出: List = [“G”, “e”, “k”, “s”]

使用 Iterator

步骤:
1. 获取有重复值的ArrayList。
2. 创建另一个ArrayList。
3. 遍历第一个数组表,使用contains()方法将每个元素的第一次出现存储到第二个数组表。
4. 第二个ArrayList包含删除了重复的元素。

下面是上述方法的实现。

// Java program to remove duplicates from ArrayList
  
import java.util.*;
  
public class GFG {
  
    // Function to remove duplicates from an ArrayList
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
    {
  
        // Create a new ArrayList
        ArrayList<T> newList = new ArrayList<T>();
  
        // Traverse through the first list
        for (T element : list) {
  
            // If this element is not present in newList
            // then add it
            if (!newList.contains(element)) {
  
                newList.add(element);
            }
        }
  
        // return the new list
        return newList;
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Get the ArrayList with duplicate values
        ArrayList<Integer>
            list = new ArrayList<>(
                Arrays
                    .asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5));
  
        // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
  
        // Remove duplicates
        ArrayList<Integer>
            newList = removeDuplicates(list);
  
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: "
                           + newList);
    }
}

输出:

ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

使用 LinkedHashSet

一个更好的方法(从时间的复杂性和实现的简易性来说)是将ArrayList中的重复部分转换为一个不允许重复的Set。因此,LinkedHashSet是最好的选择,因为它不允许重复,同时也保留了插入的顺序。

步骤:

  1. 获取有重复值的ArrayList。
  2. 从ArrayList创建一个LinkedHashSet。这将删除重复的值
  3. 将这个LinkedHashSet转换为Arraylist。
  4. 第二个ArrayList包含删除了重复的元素。

下面是上述方法的实现。

// Java program to remove duplicates from ArrayList
  
import java.util.*;
  
public class GFG {
  
    // Function to remove duplicates from an ArrayList
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
    {
  
        // Create a new LinkedHashSet
        Set<T> set = new LinkedHashSet<>();
  
        // Add the elements to set
        set.addAll(list);
  
        // Clear the list
        list.clear();
  
        // add the elements of set
        // with no duplicates to the list
        list.addAll(set);
  
        // return the list
        return list;
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Get the ArrayList with duplicate values
        ArrayList<Integer>
            list = new ArrayList<>(
                Arrays
                    .asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));
  
        // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
  
        // Remove duplicates
        ArrayList<Integer>
            newList = removeDuplicates(list);
  
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: "
                           + newList);
    }
}

输出:

ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

使用 Java 8 Stream.distinct()

你可以使用Stream API中的distinct()方法。distinct()方法根据equals()方法返回的结果,返回一个没有重复元素的新Stream,可以用于进一步处理。只有在调用forEach()或collect()等终端方法后,Stream管道的实际处理才开始。

步骤:

  1. 获取有重复值的ArrayList。
  2. 从这个ArrayList创建一个新的List。
  3. 使用Stream().distinct()方法,返回不同的对象流。
  4. 将此对象流转换为List

下面是上述方法的实现。

// Java program to remove duplicates from ArrayList
  
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
  
// Program to remove duplicates from a List in Java 8
class GFG
{
    public static void main(String[] args)
    {
        // input list with duplicates
        List<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));
            // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
  
        // Construct a new list from the set constucted from elements
        // of the original list
        List<Integer> newList = list.stream()
                                      .distinct()
                                      .collect(Collectors.toList());
  
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: "
                           + newList);
    }
}

输出:

ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程