Sitemap

How to assert that the two objects are equal in Java using comparator

3 min readJan 19, 2023

--

Press enter or click to view image in full size

In this example, we will consider comparison between two Employees represented in form of Employee class and verify that they are equal.

Example-1:

In order to check this we will create a custom comparator that compares the objects based on their attributes, for example id, name, salary, and address. Custom comparator can be written as in example below:

class Employee {
int id;
String name;
double salary;
String address;
// constructor, getters and setters
}

class EmployeeComparator implements Comparator<Employee> {
@Override
public int compare(Employee e1, Employee e2) {
if (e1.getId() != e2.getId()) {
return e1.getId() - e2.getId();
}
if (!e1.getName().equals(e2.getName())) {
return e1.getName().compareTo(e2.getName());
}
if (e1.getSalary() != e2.getSalary()) {
return (int)(e1.getSalary() - e2.getSalary());
}
if (!e1.getAddress().equals(e2.getAddress())) {
return e1.getAddress().compareTo(e2.getAddress());
}
return 0;
}
}

public class Main {
public static void main(String[] args) {
Employee e1 = new Employee(1, "John", 15000.0, "New York");
Employee e2 = new Employee(1, "John", 15000.0, "New York");
EmployeeComparator comparator = new EmployeeComparator();
if (comparator.compare(e1, e2) == 0) {
System.out.println("Both employees are equal");
} else {
System.out.println("Both employees are not equal");
}
}
}

Alternatively, we can also use assertEquals() method from JUnit library to assert that the two objects are equal.

assertEquals(e1,e2);

It will check equality based on the equals() method. So we need to override the equals() method in your Employee class accordingly.

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;
}

Also, we need to override the hashcode() method in Employee class as well if you are going to use the assertEquals() method.

Example-2:

Employee class with CompareTo() method which compares based on employee name and age.

public class Employee implements Comparable<Employee>{

String name;
int age;

public Employee(String name, int age){
this.name = name;
this.age = age;
}

@Override
public String toString(){
return this.name + " " + this.age;
}

@Override
public int compareTo(Employee o) {

if (this.age<o.age){
return -1;
}else if (this.age==o.age){
return 0;
}else {
return 1;
}
}
}
Comparator Class:

import java.util.ArrayList;
import java.util.Collections;

public class ComparableExample {

public static void main(String[] args){
Employee emp1 = new Employee("Rahul",22);
Employee emp2 = new Employee("Asish",48);
Employee emp3 = new Employee("Jamal",34);
Employee emp4 = new Employee("Waseem",40);
Employee emp5 = new Employee("Shishir",43);

//Create an Array list
ArrayList<Employee> arrayList = new ArrayList();
arrayList.add(emp1);
arrayList.add(emp2);
arrayList.add(emp3);
arrayList.add(emp4);
arrayList.add(emp5);

//Sort the List
Collections.sort(arrayList);
System.out.println(arrayList);

}
}

Conclusion:

In this way we can compare two employee objects based on their attributes using custom Comparator.

Get QA-init’s stories in your inbox

Join Medium for free to get updates from this writer.

That’s all for today. Keep supporting my work by following my page and subscribing for future updates. Thanks for Reading.!!!

--

--

QA-init
QA-init

Written by QA-init

I’m Software Engineer in Test. Finding bug is my specialization and debugging systems and process is my D2D work.