Remove Duplicate Objects from a List using Java

In this post, I will show you how to remove duplicate objects from a List using Java’s Comparator interface implementation based on multiple fields in a POJO. You can also check the duplicate objects in a list by overriding hashCode() and equals() methods in the POJO class. In other words, you are going to get the unique objects from the list and discard the duplicate objects.

Prerequisites

Java at least 8

POJO Class

Create a POJO class called Car. This class has four fields or attributes and let’s say you want to find uniqueness based on three fields. So you will determine duplicate car based on make, model and year fields.

package com.roytuts.java.remove.duplicate.objects.from.list;

public class Car {

	private String make;
	private String model;
	private int year;
	private boolean turnSignal;

	public Car() {
	}

	public Car(String make, String model, int year, boolean turnSignal) {
		this.make = make;
		this.model = model;
		this.year = year;
		this.turnSignal = turnSignal;
	}

	public String getMake() {
		return make;
	}

	public void setMake(String make) {
		this.make = make;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public boolean isTurnSignal() {
		return turnSignal;
	}

	public void setTurnSignal(boolean turnSignal) {
		this.turnSignal = turnSignal;
	}

	@Override
	public String toString() {
		return "Car [make=" + make + ", model=" + model + ", year=" + year + ", turnSignal=" + turnSignal + "]";
	}

}

Comparison

Create CarComparator class to compare each fields of the Car model class to find the duplicate Car object.

package com.roytuts.java.remove.duplicate.objects.from.list;

import java.util.Comparator;

public class CarComparator implements Comparator<Car> {

	@Override
	public int compare(Car c1, Car c2) {
		int m = c1.getMake().compareToIgnoreCase(c2.getMake());

		if (m != 0) {
			return m;
		}

		int md = c1.getModel().compareToIgnoreCase(c2.getModel());

		if (md != 0) {
			return md;
		}

		return Integer.compare(c1.getYear(), c2.getYear());
	}

}

Main Class

Now I am going to create a main class to remove the duplicate objects or to return the unique objects from a list. I am using some sample data to test the above program.

package com.roytuts.java.remove.duplicate.objects.from.list;

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

public class RemoveDuplicateObjectsApp {

	public static void main(String[] args) {
		List<Car> cars = buildCars();
		System.out.println("Cars with duplicates: " + cars.size());
		System.out.println("------------------------------");

		cars.stream().forEach(c -> System.out.println(c));

		System.out.println();

		List<Car> carWithoutDuplicates = getCarsWithoutDuplicates(cars);

		System.out.println();
		System.out.println("Cars without duplicates: " + carWithoutDuplicates.size());
		System.out.println("------------------------------");

		carWithoutDuplicates.stream().forEach(c -> System.out.println(c));
	}

	private static List<Car> getCarsWithoutDuplicates(final List<Car> cars) {
		Set<Car> carSet = new TreeSet<Car>(new CarComparator());
		for (Car car : cars) {
			carSet.add(car);
		}
		List<Car> withoutDuplicates = new ArrayList<Car>(carSet);
		return withoutDuplicates;
	}

	private static List<Car> buildCars() {
		List<Car> cars = new ArrayList<Car>();

		Car car1 = new Car("Tata Motors", "Indica", 1990, true);
		Car car2 = new Car("Tata Motors", "Sumo", 1992, true);
		Car car3 = new Car("Maruti Suzuki", "WagonR", 1990, true);
		Car car4 = new Car("Tata Motors", "Sumo", 1994, true);
		Car car5 = new Car("Tata Motors", "Indica", 1990, true);
		Car car6 = new Car("Maruti Suzuki", "Swift", 2006, true);
		Car car7 = new Car("Hundai", "SX4", 1990, true);
		Car car8 = new Car("Tata Motors", "Zica", 2015, true);
		Car car9 = new Car("Maruti Suzuki", "Dzire", 2008, true);
		Car car10 = new Car("Maruti Suzuki", "Swift", 2006, true);

		cars.add(car1);
		cars.add(car2);
		cars.add(car3);
		cars.add(car4);
		cars.add(car5);
		cars.add(car6);
		cars.add(car7);
		cars.add(car8);
		cars.add(car9);
		cars.add(car10);

		return cars;
	}

}

Testing the Program

Running the above code will give you below output. You can see that there are two duplicate Car objects and for those two objects I am returning only one.

Cars with duplicates: 10
------------------------------
Car [make=Tata Motors, model=Indica, year=1990, turnSignal=true]
Car [make=Tata Motors, model=Sumo, year=1992, turnSignal=true]
Car [make=Maruti Suzuki, model=WagonR, year=1990, turnSignal=true]
Car [make=Tata Motors, model=Sumo, year=1994, turnSignal=true]
Car [make=Tata Motors, model=Indica, year=1990, turnSignal=true]
Car [make=Maruti Suzuki, model=Swift, year=2006, turnSignal=true]
Car [make=Hundai, model=SX4, year=1990, turnSignal=true]
Car [make=Tata Motors, model=Zica, year=2015, turnSignal=true]
Car [make=Maruti Suzuki, model=Dzire, year=2008, turnSignal=true]
Car [make=Maruti Suzuki, model=Swift, year=2006, turnSignal=true]


Cars without duplicates: 8
------------------------------
Car [make=Hundai, model=SX4, year=1990, turnSignal=true]
Car [make=Maruti Suzuki, model=Dzire, year=2008, turnSignal=true]
Car [make=Maruti Suzuki, model=Swift, year=2006, turnSignal=true]
Car [make=Maruti Suzuki, model=WagonR, year=1990, turnSignal=true]
Car [make=Tata Motors, model=Indica, year=1990, turnSignal=true]
Car [make=Tata Motors, model=Sumo, year=1992, turnSignal=true]
Car [make=Tata Motors, model=Sumo, year=1994, turnSignal=true]
Car [make=Tata Motors, model=Zica, year=2015, turnSignal=true]

Overriding hashCode() and equals()

Now I will show you how to remove the duplicate objects or get the unique objects from a list by overriding hashCode() and equals() methods instead of implementing Comparator interface. So in this case you need to use HashSet instead of TreeSet which I used for removing duplicates or determining uniqueness of objects.

Add below code snippets in the Car POJO class for overriding hashCode() and equals() methods.

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((make == null) ? 0 : make.hashCode());
	result = prime * result + ((model == null) ? 0 : model.hashCode());
	result = prime * result + year;
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Car other = (Car) obj;
	if (make == null) {
		if (other.make != null)
			return false;
	} else if (!make.equals(other.make))
		return false;
	if (model == null) {
		if (other.model != null)
			return false;
	} else if (!model.equals(other.model))
		return false;
	if (year != other.year)
		return false;
	return true;
}

To get the unique objects or remove duplicate objects you can use below code snippets:

private static List<Car> getCarsWithoutDuplicates2(final List<Car> cars) {
	Set<Car> carSet = new HashSet<Car>();

	for (Car car : cars) {
		carSet.add(car);
	}

	List<Car> withoutDuplicates = new ArrayList<Car>(carSet);

	return withoutDuplicates;
}

For the above code snippets also you will get the same output as you got for the first case.

Source Code

Download

Thanks for reading.

Leave a Reply

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