Here are five different ways to remove duplicate data from an ArrayList in Java

1. Use LinkedHashSet to delete duplicate data from the ArrayList

LinkedHashSet is the best way to remove duplicate data in an ArrayList. LinkedHashSet does two things internally:

  • Deleting Duplicate Data
  • Keep the order of the data added to it

The Java example uses LinkedHashSet to remove duplicates from an ArrayList. In the given example, numbersList is an arraylist of integers, some of which are repeated numbers, such as 1,3, and 5. We add the list to the LinkedHashSet, and then return the content to the list. The resulting ArrayList has no duplicate integers.

import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); System.out.println(numbersList); LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList); ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet); System.out.println(listWithoutDuplicates); }}Copy the code

The output

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]

[1, 2, 3, 4, 5, 6, 7, 8]
Copy the code

2. Use stream, a new java8 feature, to perform List de-duplication

To remove duplicates from an ArrayList, we can also use the Java 8 Stream API. Use Steam’s distinct() method to return a stream of different data that is compared through the object’s equals () method.

Collect all area data. List Use calculator.tolist ().

A Java program used to remove duplicates from an ArrayList in Java without using Set.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); System.out.println(numbersList); List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList()); System.out.println(listWithoutDuplicates); }}Copy the code

The output

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]

[1, 2, 3, 4, 5, 6, 7, 8]
Copy the code

Since HashSet cannot guarantee the order of addition, it can only be used as a judgment condition to guarantee the order:

private static void removeDuplicate(List<String> list) {
    HashSet<String> set = new HashSet<String>(list.size());
    List<String> result = new ArrayList<String>(list.size());
    for (String str : list) {
        if (set.add(str)) {
            result.add(str);
        }
    }
    list.clear();
    list.addAll(result);
}
Copy the code

4. Use the contains method of List to loop through, reorder, add data only once, avoid repetition:

private static void removeDuplicate(List<String> list) { List<String> result = new ArrayList<String>(list.size()); for (String str : list) { if (! result.contains(str)) { result.add(str); } } list.clear(); list.addAll(result); }Copy the code

5. Double for loop de-weight

for (int i = 0; i < list.size(); i++) { 
for (int j = 0; j < list.size(); j++) { 
if(i!=j&&list.get(i)==list.get(j)) { 
list.remove(list.get(j)); 
 } 
} 
Copy the code

The author | multi-fiber jelly source | csdn.net/qq_37939251/article/details/90713643