Java Program to Compare Two Objects
Last Updated :
16 Jun, 2021
An object is an instance of a class that has its state and behavior. In java, being object-oriented, it is always dynamically created and automatically destroyed by the garbage collector as the scope of the object is over.
Illustration: An example to illustrate an object of a class:
Furniture chair=new Furniture();
Furniture sofa=new Furniture();
// Here, chair and sofa are two objects of the class Furniture
Approaches:
There are two standard methods:
- Using equals()
- Without overriding
- With overriding
- Using hashCode() and equals() method
Example 1: Although equals() method can be used to compare the values of two strings, it is not really useful by default to compare two objects without overriding it.
Java
import java.io.*;
class Pet {
String name;
int age;
String breed;
Pet(String name, int age, String breed)
{
this .name = name;
this .age = age;
this .breed = breed;
}
}
public class GFG {
public static void main(String args[])
{
Pet dog1 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat = new Pet( "Jack" , 2 , "Tabby" );
Pet dog2 = new Pet( "Snow" , 3 , "German Shepherd" );
System.out.println(dog1.equals(dog2));
}
}
|
Example 2: Overriding equals() method
Though the values of dog1 and dog2 are the same, equals() method always checks the reference of the two objects i.e if both the objects passed refer to the same object or not and not their values. Therefore, it is advisable not to use this method in comparing objects without overriding it. Implementing the equals method for the before example:
Java
import java.io.*;
class Pet {
String name;
int age;
String breed;
Pet(String name, int age, String breed)
{
this .name = name;
this .age = age;
this .breed = breed;
}
@Override public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null
|| this .getClass() != obj.getClass())
return false ;
Pet p1 = (Pet)obj;
return this .name.equals(p1.name)
&& this .age == p1.age
&& this .breed.equals(p1.breed);
}
}
public class GFG {
public static void main(String args[])
{
Pet dog1 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat = new Pet( "Jack" , 2 , "Tabby" );
Pet dog2 = new Pet( "Snow" , 3 , "German Shepherd" );
System.out.println(dog1.equals(dog2));
}
}
|
Example 3: In the above example, the equals() method is checking if all the values match or not. However, it can be overridden in any way possible i.e if one or two values match, etc. For example, if we want to check for any two values to make the equals() method consider the two objects to be the same:
Java
import java.io.*;
class Pet {
String name;
int age;
String breed;
Pet(String name, int age, String breed)
{
this .name = name;
this .age = age;
this .breed = breed;
}
@Override public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null
|| this .getClass() != obj.getClass())
return false ;
Pet p1 = (Pet)obj;
return this .name.equals(p1.name)
&& this .age == p1.age;
}
}
public class GFG {
public static void main(String args[])
{
Pet dog1 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat1 = new Pet( "Jack" , 2 , "Tabby" );
Pet dog2 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat2 = new Pet( "Jack" , 2 , "Persian" );
System.out.println(cat1.equals(cat2));
}
}
|
Using hashCode() and equals()
This method is more like an add-on to the previous one. Checking the hash values using hashCode() before entering the equals() reduces the time taken to produce the solution drastically. In this way, many comparisons between two objects need not go through the comparison of every value within them.
Example 1: The above implementation along with usage of hashCode():
Java
import java.io.*;
class Pet {
String name;
int age;
String breed;
Pet(String name, int age, String breed)
{
this .name = name;
this .age = age;
this .breed = breed;
}
@Override public int hashCode()
{
return this .name.length() % 10 ;
}
@Override public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null
|| this .getClass() != obj.getClass())
return false ;
Pet p1 = (Pet)obj;
return this .name.equals(p1.name)
&& this .age == p1.age && this .breed == p1.breed;
}
}
public class GFG {
public static void main(String args[])
{
Pet dog1 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat1 = new Pet( "Jack" , 2 , "Tabby" );
Pet dog2 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat2 = new Pet( "Jack" , 2 , "Persian" );
if (dog1.hashCode() == cat1.hashCode())
System.out.println(dog1.equals(cat1));
else
System.out.println( "Not equal" );
}
}
|
Example 2:
Java
import java.io.*;
class Pet {
String name;
int age;
String breed;
Pet(String name, int age, String breed)
{
this .name = name;
this .age = age;
this .breed = breed;
}
@Override public int hashCode()
{
return this .name.length() % 10 ;
}
@Override public boolean equals(Object obj)
{
if ( this == obj)
return true ;
if (obj == null
|| this .getClass() != obj.getClass())
return false ;
Pet p1 = (Pet)obj;
return this .name.equals(p1.name)
&& this .age == p1.age && this .breed == p1.breed;
}
}
public class GFG {
public static void main(String args[])
{
Pet dog1 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat1 = new Pet( "Jack" , 2 , "Tabby" );
Pet dog2 = new Pet( "Snow" , 3 , "German Shepherd" );
Pet cat2 = new Pet( "Jack" , 2 , "Persian" );
Pet dog3 = new Pet( "Ray" , 1 , "Siberian Husky" );
if (dog1.hashCode() == dog3.hashCode())
System.out.println(dog1.equals(dog3));
else
System.out.println( "Not equal" );
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...