从ArrayList中删除重复项 (Removing duplicates from ArrayList)
To remove duplicates from an ArrayList, there are two ways,
要从ArrayList中删除重复项,有两种方法,
With the help of HashSet
在HashSet的帮助下
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