Friday, 29 December 2017

How to Remove duplicates from ArrayList in Java

     I have shared the code to remove duplicates from ArrayList with primitive and custom objects. You can use set interface class, so it can not add elements to the class.

1) Remove duplicates from ArrayList with String as Generics.

Example:--

package com.test;

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

public class RemoveDuplicates {
 
      public static void main(String[] args) {
  
            List<String> list = new ArrayList<String>();
            list.add("Mahesh");
            list.add("Nagesh");
            list.add("Lohit");
            list.add("Mahesh"); 
            System.out.println("List with duplicates:");
            for (String name : list) {
                 System.out.println(name);
            }
  
            /* Convert list to LinkedHashSet, to preserve the insertion 
              order and remove the duplicates */
  
            Set<String> set = new LinkedHashSet<String>(list);
            System.out.println("Set with unique elements:");
            for (String name: set) {
                 System.out.println(name);
            }
      }
}

Output :--
List with duplicates:
Mahesh
Nagesh
Lohit
Mahesh
Set with unique elements:
Mahesh
Nagesh
Lohit


2) Remove duplicates from ArrayList with Custom Object as Employee

Example:--
Employee class is as below, override equals and hashCode methods.

package com.test;

public class Employee {
 
     private int empId;
     private String empName;
 
     public Employee(int empId, String empName) {
          this.empId = empId;
          this.empName = empName;
     }
 
     public int getEmpId() {
          return empId;
     }
     public void setEmpId(int empId) {
          this.empId = empId;
     }
     public String getEmpName() {
          return empName;
     }
     public void setEmpName(String empName) {
          this.empName = empName;
     }
 
     @Override
     public int hashCode() {
          return this.empId;
     }
 
     @Override
     public boolean equals(Object obj) {
          if (obj instanceof Employee) {
               return ((Employee)obj).empId == this.empId;
          }
          return false;
    }
}

Main class ,

package com.test;

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

public class RemoveCustomDuplicates {
 
      public static void main(String[] args) {
  
            Employee emp1 = new Employee(1, "Kiran");
            Employee emp2 = new Employee(2, "Mahesh");
            Employee emp3 = new Employee(3, "Lakshmi");
            Employee emp4 = new Employee(1, "Kiran");
  
            List<Employee> list = new ArrayList<Employee>();
  
            list.add(emp1);
            list.add(emp2);
            list.add(emp3);
            list.add(emp4); 
            System.out.println("List with duplicates:");
            for (Employee emp : list) {
                 System.out.println(emp.getEmpName());
            }
  
            /* Convert list to LinkedHashSet, to preserve the insertion 
             order and remove the duplicates */
  
           Set<Employee> set = new LinkedHashSet<Employee>(list);
           System.out.println("Set with unique elements:");
           for (Employee emp: set) {
                System.out.println(emp.getEmpName());
           }
      }

}

Output : --
List with duplicates:
Kiran
Mahesh
Lakshmi
Kiran
Set with unique elements:
Kiran
Mahesh
Lakshmi

Thanks for visiting blog.


Related Posts:--
1) Internal implementation of ArrayList in Java
2) Java Program to Count Occurrence of Word in a Sentence
3) How to iterate the TreeMap in reverse order in Java
4) Example to sort keys of TreeMap using Comparator with Custom Object
5) Internal implementation of ArrayList in Java

No comments:

Post a Comment