Remove Duplicates From ArrayList Java

Remove Duplicates From ArrayList Java | In this post, we will see how to remove duplicates from ArrayList in Java. We will also see how to remove duplicate objects from ArrayList in Java.

To remove duplicates from the Java List,
1) Store input values to the List.
2) Copy List to Set. Set will allow only unique elements.

In Java Set(I) stores unique elements. If we try to store duplicate elements then add() method of Set(I) returns false as result. There are three classes that implement Set(I).
1) HashSet:- It stores elements based on the hashcode value of the elements. Hashcode value is calculated based on the hashcode() method.
2) LinkedHashSet:- It stores elements in insertion order (in the order which we are inserting).
3) TreeSet:- It will store elements based on some sorting order like ascending/descending order.

Since our main task is to remove duplicates from the ArrayList, not on the order in which they will be inserted, therefore we will choose LinkedHashSet which stored elements based on insertion order.

Java Program to Remove Duplicates From ArrayList of String 

We have a list of String [Java, C++, Python, Java, C++, Java] and we want to remove all duplicates from this list. After removing duplicates from the list the expected result is:- [Java, C++, Python]

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ArrayListTest {
   public static void main(String[] args) {

      // ArrayList to store elements
      List<String> al = new ArrayList<String>();

      // store elements
      al.add("Java");
      al.add("C++");
      al.add("Python");
      al.add("Java");
      al.add("C++");
      al.add("Java");
      System.out.println("With Duplicates: " + al);

      // Convert ArrayList to LinkedHashSet
      Set<String> lhs = new LinkedHashSet<String>(al);

      // display LinkedHashSet
      System.out.println("After Removing Duplicates: " + lhs);
   }
}

Output:-

With Duplicates: [Java, C++, Python, Java, C++, Java]
After Removing Duplicates: [Java, C++, Python]

After removing duplicates, if required then we can convert LinkedHashSet to ArrayList and perform the remaining operations on the ArrayList object.

// Convert LinkedHashSet to ArrayList 
ArrayList<String> al1 = new ArrayList<String>(lhs);
System.out.println(al1); // [Java, C++, Python]

Java Program to Remove Duplicates From ArrayList of Integer values 

Here we have a list of Integer values [100, 10, 50, 100, 50, 50] and we want to remove the duplicate values from this list. The expected result after removing the duplicate elements is:- [100, 10, 50]

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ArrayListTest {
   public static void main(String[] args) {
      // ArrayList to store elements
      List<Integer> al = new ArrayList<Integer>();

      // store elements
      al.add(100);
      al.add(10);
      al.add(50);
      al.add(100);
      al.add(50);
      al.add(50);
      System.out.println("With Duplicates: " + al);

      // Convert ArrayList to LinkedHashSet
      Set<Integer> lhs = new LinkedHashSet<Integer>(al);
      // display LinkedHashSet
      System.out.println("After Removing Duplicates: " + lhs);
   }
}

Output:-

With Duplicates: [100, 10, 50, 100, 50, 50]
After Removing Duplicates: [100, 10, 50]

How LinkedHashSet class is able to remove duplicates? Before inserting elements HashSet and LinkedHashSet class use equals() method to compare with existing all elements. If the equals() method returns true then the element is unique therefore it will be inserted to the Set but when the equals() method returns false then the element is duplicate hence that element will not be inserted into the Set.

In String, and all wrapper classes (Integer, Double, Float, Long, and e.t.c.) equals() method is overridden for content comparison. Therefore while inserting the String element, the given string will be compared with all existing String. If all comparisons return true then the element is unique and will be inserted, but if at least one comparison returns false then String will not be inserted.

Java Remove Duplicates From List of Objects

We have a list of objects as given in the below table. The list contains different products, among them we have to remove the duplicate products. Each product has its PID (product id), name (product name), qty (quantity), and price. 

Products List

PidNameQuantityPrice
123Laptop152000.0
154Mobile30200.0
543Keyboard2030.0
123Laptop152000.0
154Mobile50500.0
543Keyboard2030.0

A product will be called duplicate if all properties matched with some other product. It means duplicate products will have the same PID, name, quantity, and price. We need to write a Java program to remove duplicates from the product list.

For this we have to override the equals() method in the Product class and compare PID, name, quantity, and price. Whenever we override the equals() method then we also need to override the hashCode() method. See more:- equals() method in Java, hashCode() in Java.

import java.util.Objects;

class Product {

   private int pid;
   private String name;
   private int qty; // quantity
   private double price;

   public Product(int pid, String name, 
                  int qty, double price) {
      this.pid = pid;
      this.name = name;
      this.qty = qty;
      this.price = price;
   }

   @Override
   public String toString() {
      return "{" + pid + ", " + name + ", " 
             + qty + ", " + price + "}\n";
   }

   @Override
   public int hashCode() {
      return Objects.hash(name, pid, price, qty);
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Product other = (Product) obj;
      return name.equals(other.name) 
             && pid == other.pid 
             && price == other.price 
             && qty == other.qty;
   }
}

In the Product class, we have overridden the toString() method to display the Products. The hashCode() method is internally using hash() method of java.util.Objects class which generates a hash code for a sequence of input values.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Shop {
   public static void main(String[] args) {

      // list to store products
      List<Product> al = new ArrayList<Product>();
      
      // adding products to set
      al.add(new Product(123, "Laptop", 15, 2000.0));
      al.add(new Product(154, "Mobile", 30, 200.0));
      al.add(new Product(543, "Keyboard", 20, 30.0));
      al.add(new Product(123, "Laptop", 15, 2000.0)); 
      al.add(new Product(154, "Mobile", 50, 500.0));
      al.add(new Product(543, "Keyboard", 20, 30.0));
      System.out.println("With Duplicates:\n" + al);
      
      // LinkedHashSet
      Set<Product> lhs = new LinkedHashSet<Product>(al);
      System.out.println("After Removing Duplicates:\n" 
                                              + lhs);
   }
}

Output:-

With Duplicates:
[{123, Laptop, 15, 2000.0}
, {154, Mobile, 30, 200.0}
, {543, Keyboard, 20, 30.0}
, {123, Laptop, 15, 2000.0}
, {154, Mobile, 50, 500.0}
, {543, Keyboard, 20, 30.0}
]
After Removing Duplicates:
[{123, Laptop, 15, 2000.0}
, {154, Mobile, 30, 200.0}
, {543, Keyboard, 20, 30.0}
, {154, Mobile, 50, 500.0}
]

Remove Duplicates From List Java 8

We can use Java 8 Stream to remove duplicates from the list. Here we can use Stream().distinct() method which return distinct object stream. After that convert this object stream into a List.

The distinct() method returns a new Stream without duplicates elements based on the result returned by the equals() method, which can be used for further processing. The actual processing of the Stream pipeline starts only after calling terminal methods like forEach() or collect().

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListTest {
   public static void main(String[] args) {

      // ArrayList to store elements
      List<Integer> al = new ArrayList<Integer>();

      // store elements
      al.add(100);
      al.add(10);
      al.add(50);
      al.add(100);
      al.add(50);
      al.add(50);
      System.out.println("With Duplicates: " + al);
      
      List<Integer> newList = 
         al.stream().distinct().collect(Collectors.toList());
      System.out.println("After Removing Duplicates: "
                                          + newList);
   }
}

Output:-

With Duplicates: [100, 10, 50, 100, 50, 50]
After Removing Duplicates: [100, 10, 50]


If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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