Method to delete duplicate elements in ArrayList in Java
In Java programming, ArrayList is a commonly used collection class that allows us to store a set of elements. In some cases, we may need to remove the duplicate elements in it, leaving only the unique elements. The following are two common ways to delete duplicate elements in ArrayList.
Method 1: Use the properties of Set collections
Java'sSet
The collection does not allow duplicate elements, so we can temporarily store the elements in the ArrayList into the Set, and then put the elements in the Set back into the ArrayList.
import ; import ; import ; import ; public class RemoveDuplicatesFromArrayList { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 10); ("Original List: " + numbers); // Use Set collection to remove duplicate elements Set<Integer> uniqueNumbers = new HashSet<>(numbers); (); (uniqueNumbers); ("Remove duplicate elements list: " + numbers); } }
In this method, we first create a HashSet which is an implementation class of Set, and then we add the elements from the ArrayList to this Set. Since Set does not allow duplicate elements, this process automatically removes duplicate elements. Finally, we add the elements from the Set back to the ArrayList.
Method 2: Use and Iterator
Java's Collections class provides a frequent method that returns the number of times an element appears in the collection. We can use this method to iterate over the ArrayList and remove elements that appear more than 1 times.
import ; import ; import ; import ; public class RemoveDuplicatesFromArrayList { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 10); ("Original List: " + numbers); // Use Iterator and remove duplicate elements Iterator<Integer> iterator = (); while (()) { Integer number = (); if ((numbers, number) > 1) { (); } } ("Remove duplicate elements list: " + numbers); } }
In this method, we use an Iterator to iterate over the ArrayList, and for each element we check how many times it appears in the collection. If the number of times exceeds 1, then we know that this element is repeated, so we remove it through the() method.
Both methods can effectively remove duplicate elements from ArrayList, but the first method may be more efficient because it directly takes advantage of the Set's properties, while the second method requires iterating over the set twice (one to check for duplicates, one to remove). In practical applications, appropriate methods should be selected according to the specific scenario. In practice, you may need to delete duplicate elements in ArrayList based on specific conditions. Here are two common methods:
Method 1: Use the removeIf method of the collection
The Java collection's removeIf method allows you to determine whether to remove elements based on the provided Predicate conditions. Here is an example:
import ; import ; import ; public class RemoveDuplicates { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 10, 10, 10); List<Integer> uniqueNumbers = removeDuplicates(numbers); (uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } public static List<Integer> removeDuplicates(List<Integer> numbers) { // Use Stream API and removeIf methods to remove duplicate elements return () .distinct() .collect(()); } }
In this example, we use the distinct() operation to remove duplicate elements. This method creates a new collection that contains copies of all unique elements in the original collection.
Method 2: Use the remove method of the collection
If you want to remove duplicate elements on the original collection and you have a clear criteria for judging duplicates, you can use the remove method. Here is an example:
import ; import ; public class RemoveDuplicates { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 10, 10, 10); List<Integer> uniqueNumbers = removeDuplicatesUsingRemove(numbers); (uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } public static List<Integer> removeDuplicatesUsingRemove(List<Integer> numbers) { // Use a loop to remove duplicate elements for (int i = 0; i < () - 1; i++) { for (int j = i + 1; j < (); j++) { if ((i) == (j)) { (j); j--; // Adjust the loop index, because the remove operation will change the size of the collection } } } return numbers; } }
In this example, we use a double loop to check if each element is repeated with subsequent elements. If repeated, we use the remove method to remove the element. This method changes the content of the original collection.
Note that the above method assumes that you want to remove duplicates based on element values. If your elements are objects, you may need to compare the equals methods of objects to determine if they are equal. In Java, to remove duplicate elements from ArrayList, you can use the() and() methods, or use the properties of the Set collection. Here are detailed code examples of these two methods:
Method 1: Use() and()
This approach relies on the trick of sorting and deleting adjacent duplicate elements. First, sort the list, then iterate over the list, comparing the current element to the next element each time, and if the same, delete the next element.
import ; import ; import ; public class RemoveDuplicates { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 7); removeDuplicatesUsingSort(numbers); (numbers); // Output: [1, 2, 3, 4, 5, 6, 7] } public static <T> void removeDuplicatesUsingSort(List<T> list) { // Sort list (list); // traverse the list and delete duplicate elements int i = 0; while (i < () - 1) { if ((i).equals((i + 1))) { (i + 1); } else { i++; } } } }
Method 2: Use Set Collection
This method is to add elements from ArrayList to a Set, because the Set does not allow duplicate elements, so the process itself removes duplicate elements. Then, you add the elements in the Set back to the ArrayList.
import ; import ; import ; import ; public class RemoveDuplicates { public static void main(String[] args) { List<Integer> numbers = (1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 7); removeDuplicatesUsingSet(numbers); (numbers); // Output: [1, 2, 3, 4, 5, 6, 7] } public static <T> void removeDuplicatesUsingSet(List<T> list) { // Create a Set and add all elements in the ArrayList Set<T> set = new HashSet<>(); (list); // Add elements in Set back to ArrayList (); (set); } }
Note that the above code assumes that elements can be compared efficiently (for example, they implementComparable
Interface or you can provide a comparator). If the elements are incomparable, you need to provide a custom comparator or use other methods to uniqueize the elements.
The above is the detailed content of two methods for Java to delete duplicate elements in ArrayList. For more information about Java to delete duplicate elements of ArrayList, please follow my other related articles!