如何从Java中的ArrayList中删除重复项?

从ArrayList中删除重复项 (Removing duplicates from ArrayList)

To remove duplicates from an ArrayList, there are two ways,

要从ArrayList中删除重复项,有两种方法,

  1. With the help of HashSet

    在HashSet的帮助下

  2. With the help of LinkedHashSet

    借助LinkedHashSet

1)使用HashSet删除重复项 (1) Remove duplicates using HashSet)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • By using HashSet class, we can remove the duplicate element from the ArrayList.

    通过使用HashSet类,我们可以从ArrayList中删除重复的元素。

  • In case of HashSet, After removing the duplicate elements, The insertion order of the elements is not preserved (i.e. The retrieval order of the elements is not needed to be the same as the insertion order).

    如果是HashSet,则在删除重复元素之后,将不保留元素的插入顺序(即,元素的检索顺序不需要与插入顺序相同)。

The process of removing duplicate elements from ArrayList using HashSet:

使用HashSet从ArrayList中删除重复元素的过程:

  • Copy ArrayList elements to HashSet.

    将ArrayList元素复制到HashSet。

  • After copying, clear ArrayList by using the clear() method.

    复制后,使用clear()方法清除ArrayList。

  • Again copy HashSet elements to ArrayList.

    再次将HashSet元素复制到ArrayList。

Example:

例:

// Java program to demonstrate the example of
// removing duplicate element from ArrayList 
// by using HashSet.

import java.util.*;

public class RemovedDuplicateFromArrayList {
    public static void main(String[] args) {
        // ArrayList Declaration
        ArrayList al = new ArrayList();

        // By using add() method to add few elements in 
        // ArrayList
        al.add(10);
        al.add(10);
        al.add(20);
        al.add(20);
        al.add(30);

        // Display ArrayList with duplicates
        System.out.print("Display ArrayList with duplicates : " + " ");
        System.out.println(al);

        // HashSet Declaration
        HashSet hs = new HashSet();

        // By using addAll() method is to add all elements
        // to HashSet

        hs.addAll(al);

        // By using clear() method is to clear the ArrayList
        al.clear();

        // Again by using addAll() method is to add all elements
        // to ArrayList
        al.addAll(hs);

        // Display ArrayList with no duplicates
        System.out.print("Display ArrayList with no duplicates : " + " ");
        System.out.println(al);
    }
}

Output

输出量

Display ArrayList with duplicates :  [10, 10, 20, 20, 30]
Display ArrayList with no duplicates :  [20, 10, 30]

2)使用LinkedHashSet删除重复项 (2) Remove duplicates using LinkedHashSet)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • By using LinkedHashSet class, we can remove the duplicate element from the ArrayList.

    通过使用LinkedHashSet类,我们可以从ArrayList中删除重复的元素。

  • In the case of LinkedHashSet, After removing the duplicate elements, The insertion order of the elements is preserved (i.e. The retrieval order of the elements is needed to be the same as the insertion order).

    对于LinkedHashSet,在删除重复的元素之后,将保留元素的插入顺序(即,元素的检索顺序必须与插入顺序相同)。

The process of removing duplicate elements from ArrayList using LinkedHashSet:

使用LinkedHashSet从ArrayList中删除重复元素的过程:

  • Copy ArrayList elements to LinkedHashSet.

    将ArrayList元素复制到LinkedHashSet。

  • After copying, clear ArrayList by using the clear() method.

    复制后,使用clear()方法清除ArrayList。

  • Again copy LinkedHashSet elements to ArrayList.

    再次将LinkedHashSet元素复制到ArrayList。

Example:

例:

// Java program to demonstrate the example of
// removing duplicate element from ArrayList 
// by using LinkedHashSet.

import java.util.*;

public class RemovedDuplicateFromArrayList {
    public static void main(String[] args) {
        // ArrayList Declaration
        ArrayList al = new ArrayList();

        // By using add() method to add few elements in 
        // ArrayList
        al.add(10);
        al.add(10);
        al.add(20);
        al.add(20);
        al.add(30);

        // Display ArrayList with duplicates
        System.out.print("Display ArrayList with duplicates : " + " ");
        System.out.println(al);

        // LinkedHashSet Declaration
        LinkedHashSet lhs = new LinkedHashSet();

        // By using addAll() method is to add all elements
        // to LinkedHashSet
        lhs.addAll(al);

        // By using clear() method is to clear the ArrayList
        al.clear();

        // Again by using addAll() method is to add all elements
        // to ArrayList
        al.addAll(lhs);

        // Display ArrayList with no duplicates
        System.out.print("Resultant ArrayList : " + " ");
        System.out.println(al);
    }
}

Output

输出量

Display ArrayList with duplicates :  [10, 10, 20, 20, 30]
Resultant ArrayList :  [10, 20, 30]


翻译自: https://www.includehelp.com/java/remove-duplicates-from-arraylist-in-java.aspx

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值