StackTips

How to Remove Duplicate in ArrayList using Comparator

nilan avtar

Written by

Nilanchala,  3 min read,  3.61K views, updated on Sept. 17, 2023

This example shows how to remove duplicate from ArrayList using Comparator. The easiest way to remove duplicate is by passing the List to an Set. We will use Comparator to remove duplicate elements. Once you have the Set you can again pass it back to ArrayList.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class RemoveDuplicate {

	public static void main(String[] args) {

		final ArrayList students = new ArrayList();
		students.add(new Student("Student1", "1000"));
		students.add(new Student("Student2", "1001"));
		students.add(new Student("Student3", "1002"));
		students.add(new Student("Student4", "1003"));
		students.add(new Student("Student5", "1001"));
		students.add(new Student("Student6", "1004"));

		/** Printing original list **/
		System.out.println(students);

		Set set = new TreeSet(new Comparator() {
			@Override
			public int compare(Student o1, Student o2) {
				if(o1.getId().equalsIgnoreCase(o2.getId())){
	        		return 0;
	        	}
	        	return 1;
			}
		});
		set.addAll(students);

		System.out.println("\n***** After removing duplicates *******\n");

	    final ArrayList newList = new ArrayList(set);

	    /** Printing original list **/
		System.out.println(newList);
	}

}

class Student {
	private String name;
	private String id;

	public Student(String name, String id) {
		this.name = name;
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "\n" +"Name=" + name + "   Id=" + id ;
	}
}

Output

[
Name=Student1   Id=1000, 
Name=Student2   Id=1001, 
Name=Student3   Id=1002, 
Name=Student4   Id=1003, 
Name=Student5   Id=1001, 
Name=Student6   Id=1004]

***** After removing duplicates *******

[
Name=Student1   Id=1000, 
Name=Student2   Id=1001, 
Name=Student3   Id=1002, 
Name=Student4   Id=1003, 
Name=Student6   Id=1004]

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!