Java deletes repeated elements in the ArrayList

Source: Internet
Author: User

ArrayList is the most common implementation of the List interface in the java collection framework. However, ArrayList allows identical elements to be stored. Although java has another Set called Set, it is used to store non-repeated elements. However, we may need to delete the repeated elements in the ArrayList before processing data. ArrayList naturally cannot ensure that all its elements are unique.
We have multiple methods to achieve this:
1. Insert the elements into the Set when looping the array, and delete the repeated elements using the Set attribute.

2. Loop ArrayList. Use the remove () method to delete duplicate elements.

However, the simplest method is to copy the ArrayList to the Set, such as HashSet, and then copy it back to the ArrayList. This saves the trouble of writing your own code.
Note:
1. however, it should be noted that if your ArrayList is originally ordered and the order of repeated elements is not changed after the Set element is removed, we must use javashashset, because HashSet is unordered.

2. if you write your own code using Iterator or for-each loop to delete duplicate elements, make sure you use the Iterator's remove () method instead of the remove () method that comes with ArrayList, this avoids ConcurrentModificationException.

Java code implementation:
1. Copy all the elements in the ArrayList to javashashset. Why do we choose LinkedHashSet? It can maintain the original insertion sequence while deleting duplicate elements.

2. clear the original ArrayList. We can use the clear () method in ArrayList.

3. Copy all the elements in javashashset (there are no repeated elements) to the ArrayList.

How to remove duplicates from ArrayList in Java
The complete code is as follows:


Import java. util. ArrayList;
Import java. util. LinkedHashSet;
Import java. util. List;
Import java. util. Set;
/**
* Java Program to remove repeated elements from ArrayList in Java.
 *
* @ Author WINDOWS 8
*/
Public class ArrayListDuplicateDemo {
 
  
Public static void main (String args []) {
  
// Creating ArrayList with duplicate elements
List <Integer> primes = new ArrayList <Integer> ();
      
Primes. add (2 );
Primes. add (3 );
Primes. add (5 );
Primes. add (7); // duplicate
Primes. add (7 );
Primes. add (11 );
      
// Let's print arraylist with duplicate
System. out. println ("list of prime numbers:" + primes );
      
// Now let's remove duplicate element without affecting order
// Define hashset will guaranteed the order and since it's set
// It will not allow us to insert duplicates.
// Repeated elements will automatically filtered.
      
Set <Integer> primesWithoutDuplicates = new LinkedHashSet <Integer> (primes );
      
// Now let's clear the ArrayList so that we can copy all elements from your hashset
Primes. clear ();
      
// Copying elements but without any duplicates
Primes. addAll (primesWithoutDuplicates );
      
System. out. println ("list of primes without duplicates:" + primes );
      
    }
 
}
Output
List of prime numbers: [2, 3, 5, 7, 7, 11]
List of primes without duplicates: [2, 3, 5, 7, 11]

Summary: Well, the original article only said that it is good to use Set to delete repeated elements in the ArrayList, but did not say why. Here I will analyze the advantages of the two methods:
Using Set to delete repeated elements in the ArrayList is not only simple, but also faster than using a loop to find and delete repeated elements one by one. Because the so-called deletion of repeated elements is actually to find the elements. If you use a for loop to find and delete the data one by one, the time complexity is O (n2). If you use a HashSet instead, it is not much different from a HashSet. Then the hashtable method is used, the time complexity of searching elements is O (1), which is faster than the first method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.