SoFunction
Updated on 2025-03-01

Java method to compare whether two objects are equal

Things to note

  1. Use the equals method: For basic data types and wrapper classes, you can directly use the == operator for comparison. For objects, equals should be used
    Methods are compared because the equals method takes into account the actual content of the object, while the == operator compares the reference to the object.

  2. Handling null values: When comparing objects, you need to consider the null value to avoid NullPointerException.

  3. Comparing sets: Comparing sets usually requires considering the order and number of elements in the set.

Common tools

  • Apache Commons Lang library

Comparative principle:

In Java, the "equal" of a method refers to two objects returning true by comparing their equals method. This is a standard definition of object equality in Java, and it is determined whether they are the same based on the content or state of the object.

When using the method, the following situations apply:

If both object references point to the same object, they are considered equal.

If both objects are null, they are considered equal.

If one of the objects is null and the other is not, they are considered unequal.

If neither object is null, their equals method is called. If the equals method returns true, the two objects are considered equal; if false, they are considered unequal.

The advantage of this approach is that it avoids the NullPointerException that can occur when calling the equals method directly. If one of the objects is null, false will be returned immediately without attempting to call the equals method.

  1. Using Java 8

The Objects class introduced in Java 8 provides an equals method that can also safely compare two objects.

Here is an example of using the Apache Commons Lang package method to compare whether two objects are equal:

String str1 = "hello";
String str2 = "world";
String str3 = "hello";

boolean equals = (str1, str3); // Use the Apache Commons Lang library
  • Compare Integer
Integer num1 = 42;
Integer num2 = 42;
Integer num3 = null;

boolean equals = (num1, num2); // Use the Apache Commons Lang library
  • Compare List
List<String> list1 = ("a", "b", "c");
List<String> list2 = ("a", "b", "c");
List<String> list3 = null;

boolean equals = (list1, list2); // Use the Apache Commons Lang library
  • Compare custom objects
    For custom objects, you need to make sure your class implements the equals method correctly.
class Person {
    private String name;
    private int age;

    // ... getters and setters ...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != ()) return false;
        Person person = (Person) o;
        return age ==  &&
                (name, );
    }

    @Override
    public int hashCode() {
        return (name, age);
    }
}

Person person1 = new Person("Alice", 30);
Person person2 = new Person("Alice", 30);
Person person3 = null;

boolean equals = (person1, person2); // Use the Apache Commons Lang library
  1. How to use Hutool package

Hutool is a small and complete Java tool class library that provides some tooling methods to simplify code development. In Hutool, the ObjectUtil class can be used to compare objects equality.

Here is an example of using the ObjectUtil class in Hutool to compare two objects:

import ;

public class HutoolEqualsExample {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "world";
        String str3 = "hello";
        String str4 = null;

        boolean equals = (str1, str3); // Compare str1 and str3        boolean notEquals = (str1, str2); // Compare str1 and str2        boolean nullSafe = (str1, str4); // Compare str1 and null
        ("Equals: " + equals); // Output true        ("Not Equals: " + notEquals); // Output false        ("Null Safe: " + nullSafe); // Output false    }
}

Hutool's method is similar to the sum, which can safely compare whether two objects are equal while handling the case of null values. When both objects are null, it returns true; when one is null and the other is not, it returns false; when neither object is null, it calls the equals method of the object for comparison.

Note that to use the Hutool library, you need to add Hutool dependencies to your project. If you use Maven, you can add the following dependencies to the file:

<dependency>
    <groupId></groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.16</version> <!-- Please use the latest version -->
</dependency>

This is the end of this article about how Java compares whether two objects are equal. For more related Java comparison content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!