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

  1. Delete duplicate data from the ArrayList using LinkedHashSet

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.

For example, 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
  1. List de-duplication using stream, a new java8 feature

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
  1. Since HashSet does not 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
  1. Use the contains method of List to loop through, reorder, add data only once, avoid duplication:
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
  1. Double for loop for deweighting
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