beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to remove repeated elements from ArrayList?

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we will learn how to remove duplicates from ArrayList. The steps followed in the below example are:
1) Copying all the elements of ArrayList to LinkedHashSet. Why we choose LinkedHashSet? Because it removes duplicates and maintains the insertion order.
2) Emptying the ArrayList
3) Copying all the elements of LinkedHashSet(non-duplicate elements) to the ArrayList. Please find below the complete code.

Example: Removing duplicates from List

import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashSet;
public class RemoveDuplicates {

  public static void main(String[] args) {
 
     /* Creating ArrayList of Strings and adding
      * elements to it
      */
     List<String> al = new ArrayList<String>();
     al.add("Ajay");
     al.add("Becky");
     al.add("Chaitanya");
     al.add("Ajay");
     al.add("Rock");
     al.add("Becky");
 
     // Displaying ArrayList elements 
     System.out.println("Before:");
     System.out.println("ArrayList contains: "+al);
 
     // Creating LinkedHashSet
     LinkedHashSet<String> lhs = new LinkedHashSet<String>();
 
     /* Adding ArrayList elements to the LinkedHashSet
      * in order to remove the duplicate elements and 
      * to preserve the insertion order.
      */
     lhs.addAll(al);
  
     // Removing ArrayList elements
     al.clear();
 
     // Adding LinkedHashSet elements to the ArrayList
     al.addAll(lhs);
 
     // Displaying ArrayList elements
     System.out.println("After:");
     System.out.println("ArrayList contains: "+al);
  }
}

Output:

Before:
ArrayList contains: [Ajay, Becky, Chaitanya, Ajay, Rock, Becky]
After:
ArrayList contains: [Ajay, Becky, Chaitanya, Rock]

As you can see that the duplicates have been removed from the list and the insertion order is being preserved in the output.

Thats all we have for this tutorial. If you have any questions, feel free to ask by dropping a comment below.

Enjoyed this post? Try these related posts

  1. How to convert Vector to String array in java
  2. Get size of Hashtable example in Java
  3. LinkedHashSet Class in Java with Example
  4. How to serialize ArrayList in java
  5. Difference between Iterator and ListIterator in java
  6. Java – Remove element from a specific index in LinkedList example

Comments

  1. yogesh says

    October 24, 2014 at 10:30 AM

    How to check whether the ArrayList contains un-ordered element or not with an in-build method?
    eg.
    Suppose i have a strings as “ABC” and “BCA”
    and if i check this ans should be true!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap