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:--
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.
Main class ,
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
1) Remove duplicates from ArrayList with String as Generics.
Example:--
package com.test;
importjava.util.ArrayList;
importjava.util.LinkedHashSet;
importjava.util.List;
importjava.util.Set;
publicclassRemoveDuplicates {
publicstaticvoidmain(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;
publicclassEmployee {
privateint empId;
private String empName;
publicEmployee(int empId, String empName) {
this.empId = empId;
this.empName = empName;
}
publicintgetEmpId() {
return empId;
}
publicvoidsetEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
publicvoidsetEmpName(String empName) {
this.empName = empName;
}
@Override
publicinthashCode() {
returnthis.empId;
}
@Override
publicbooleanequals(Object obj) {
if (obj instanceof Employee) {
return ((Employee)obj).empId == this.empId;
}
returnfalse;
}
}
Main class ,
package com.test;
importjava.util.ArrayList;
importjava.util.LinkedHashSet;
importjava.util.List;
importjava.util.Set;
publicclassRemoveCustomDuplicates {
publicstaticvoidmain(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