Here are five different methods to remove duplicate data from an ArrayList in Java: 1
LinkedHashSet is the best way to remove duplicate data in an ArrayList. LinkedHashSet does two things internally: Deleting duplicates Preserves the order of data added to it. The Java example uses LinkedHashSet to remove duplicates in 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
} Output results [1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8
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
} Output results [1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]
private static void removeDuplicate(List list) { HashSet set = new HashSet(list.size()); List result = new ArrayList(list.size()); for (String str : list) { if (set.add(str)) { result.add(str); } } list.clear(); list.addAll(result); }
4, use the contains method of List loop through, reorder, only add data once, avoid repetition:
private static void removeDuplicate(List list) { List result = new ArrayList(list.size()); for (String str : list) { if (! result.contains(str)) { result.add(str); } } list.clear(); list.addAll(result); }
5. Double for loop 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)); } }
Dry goods sharing:
Recently, I organized my personal study notes into a book and shared it with PDF. Pay attention to me, reply to the following code, you can get baidu plate address, no routine to get! Add wechat: Gogquick
•001: Study notes on Java Concurrency and High Concurrency Solutions •002: Study notes on “Deep into the JVM Kernel — Principles, Diagnostics, and Optimization” •005: Kubernetes open-source Book •006: DDD Crash Course (Dome-driven Design Crash Course)