Home > Java > javaTutorial > body text

Tutorial on how to use the equals() method to determine whether objects are equal in Java

高洛峰
Release: 2017-02-03 13:08:57
Original
3064 people have browsed it

The equals method in the Object class is used to detect whether an object is equal to another object. In the Object class, this method determines whether two objects have the same reference. If two objects have the same reference, they must be equal. From this point of view, it makes sense to make this the default action. However, for most classes, this judgment does not make sense. For example, it is completely meaningless to compare two PrintStreams in this way. However, it is often necessary to detect the equality of the states of two objects. If the states of two objects are equal, the two objects are considered equal. Therefore, equals comparison must be overridden in custom classes.

The following are suggestions for writing a perfect equals() method:

(1) The explicit parameter is named otherObject, which needs to be converted into a variable called other later

(2) Check whether this and otherObject refer to the same object:

if(this==otherObject) return true;
Copy after login

This statement is just an optimization. In fact, this is a form that is often taken. Because computing this equation is much less expensive than comparing the fields in the class one by one.

(3) Check whether otherObject is null. If it is null, return false. This test is very necessary.

if(otherObject==null) return false;
Copy after login

(4) Compare this and otherObject to see if they belong to the same class. If the semantics of equals change in each subclass, use getClass() to detect it, and it will treat itself as Target class

if(getClass()!=otherObject.getClass()) return false;
Copy after login

If all subclasses have the same semantics, use instanceof to detect

if(!(otherObject instanceof ClassName)) return false;
Copy after login

(5) Convert otherObject to Variables of the corresponding type:

ClassName other=(ClassName)otherObject;
Copy after login

(6) Now start comparing all fields that need to be compared. Use == to compare basic type fields and equals to compare object fields. If all fields match, return true, otherwise return false;

return field1==other.field1&&field2.equals(other.field2)
Copy after login

If equals is redefined in a subclass, it must include calling super.equals(other). If the test fails, equality is impossible. If the fields in the superclass are equal, the instance fields in the subclass are compared.

For array type fields, you can use the static Arrays.equals method to check whether the corresponding elements are equal.

Let’s look at a few string comparison examples:

String a = "abc";
String b = "abc";
String c = new String("abc");
String d = new String("abc");
System.out.println(a == b); // true 因为JAVA中字符串常量是共享的,只有一个拷贝
System.out.println(a == c); // false a和c属于2个不同的对象
System.out.println(a.equals(c)); // true 由于String对象的equals方法比较的是对象中的值,所以返回true。(和Object的equals方法不同)
System.out.println(c==d); // false c和d虽然对象内的值相同,但属于2个不同的对象,所以不相等
System.out.println(c.equals(d)); // true
Copy after login

Simply put, when comparing string constants, the result returned by equals is the same. When you want to compare Use equals when using the value of a string object.

Look at an example of using equals:

package chapter05.EqualsTest;
  
import java.util.*;
  
public class EqualsTest {
 public static void main(String[] args) {
  Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
  Employee alice2 = alice1; // reference the same object
  Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
  Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
  
  System.out.println("alice1 == alice2: " + (alice1 == alice2));
  
  System.out.println("alice1 == alice3: " + (alice1 == alice3));
  
  System.out.println("alice1.equals(alice3): " + (alice1.equals(alice3)));
  
  System.out.println("alice1.equals(bob): " + (alice1.equals(bob)));
  
  System.out.println(bob.toString());
 }
}
  
class Employee {
 public Employee(String n, double s, int year, int month, int day) {
  name = n;
  salary = s;
  GregorianCalendar calendar = new GregorianCalendar(year, month, day);
  hireDay = calendar.getTime();
 }
  
 public String getName() {
  return name;
 }
  
 public double getSalary() {
  return salary;
 }
  
 public Date getHireDay() {
  return hireDay;
 }
  
 public void raiseSalary(double byPercent) {
  double raise = salary * byPercent / 100;
  salary += raise;
 }
  
 @Override
 public boolean equals(Object otherObject) {
  // a quick test to see if the objects are identical
  if (this == otherObject)
   return true;
  
  // must return false if the explicit parameter is null
  if (otherObject == null)
   return false;
  
  // if the classed don't match,they can't be equal
  if (getClass() != otherObject.getClass())
   return false;
  
  // now we know otherObject is a non-null Employee
  Employee other = (Employee) otherObject;
  
  // test whether the fields hava identical values
  return name.equals(other.name) && salary == other.salary
    && hireDay.equals(other.hireDay);
  
 }
  
 @Override
 public int hashCode() {
  return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13
    * hireDay.hashCode();
 }
  
 @Override
 public String toString() {
  return getClass().getName() + "[name=" + name + ",salary=" + salary
    + ",hireDay=" + hireDay + "]";
 }
  
 private String name;
 private double salary;
 private Date hireDay;
}
  
class Manager extends Employee {
 public Manager(String n, double s, int year, int month, int day) {
  super(n, s, year, month, day);
  bouns = 0;
 }
  
 @Override
 public double getSalary() {
  double baseSalary = super.getSalary();
  return baseSalary + bouns;
 }
  
 public void setBouns(double b) {
  bouns = b;
 }
  
 @Override
 public boolean equals(Object otherObject) {
  if (!super.equals(otherObject))
   return false;
  Manager other = (Manager) otherObject;
  // super equals checked that this and other belong to the same class
  return bouns == other.bouns;
 }
  
 @Override
 public int hashCode() {
  return super.hashCode() + 17 * new Double(bouns).hashCode();
 }
  
 @Override
 public String toString() {
  return super.toString() + "[bouns=" + bouns + "]";
 }
  
 private double bouns;
}
Copy after login

Go deeper
The following is divided into 2 categories based on "whether the class covers the equals() method".
(1) If a class does not cover the equals() method, when it compares two objects through equals(), it actually compares whether the two objects are the same object. At this time, it is equivalent to comparing the two objects through "==".
(2) We can override the equals() method of the class to let equals() compare whether two objects are equal through other methods. The usual approach is: if the contents of the two objects are equal, the equals() method returns true; otherwise, returns fasle.
Below, give examples to illustrate the above two situations.
1. "The equals() method is not covered"
The code is as follows (EqualsTest1.java):

import java.util.*;
import java.lang.Comparable;
 
/**
 * @desc equals()的测试程序。
 
 */
public class EqualsTest1{
 
 public static void main(String[] args) {
  // 新建2个相同内容的Person对象,
  // 再用equals比较它们是否相等
  Person p1 = new Person("eee", 100);
  Person p2 = new Person("eee", 100);
  System.out.printf("%s\n", p1.equals(p2));
 }
 
 /**
  * @desc Person类。
  */
 private static class Person {
  int age;
  String name;
 
  public Person(String name, int age) {
   this.name = name;
   this.age = age;
  }
 
  public String toString() {
   return name + " - " +age;
  }
 }
}
Copy after login

Running result:

false
Copy after login


Result Analysis
We use p1.equals(p2) to "compare whether p1 and p2 are equal." In fact, the equals() method of Object.java is called, that is, (p1==p2) is called. It compares "whether p1 and p2 are the same object".
It can be seen from the definitions of p1 and p2 that although they have the same content, they are two different objects! Therefore, the return result is false.

2. The case of "overriding the equals() method"
We modify the above EqualsTest1.java: override the equals() method.
The code is as follows (EqualsTest2.java):

import java.util.*;
import java.lang.Comparable;
 
/**
 * @desc equals()的测试程序。
 */
public class EqualsTest2{
 
 public static void main(String[] args) {
  // 新建2个相同内容的Person对象,
  // 再用equals比较它们是否相等
  Person p1 = new Person("eee", 100);
  Person p2 = new Person("eee", 100);
  System.out.printf("%s\n", p1.equals(p2));
 }
 
 /**
  * @desc Person类。
  */
 private static class Person {
  int age;
  String name;
 
  public Person(String name, int age) {
   this.name = name;
   this.age = age;
  }
 
  public String toString() {
   return name + " - " +age;
  }
 
  /**
   * @desc 覆盖equals方法
   */
  @Override
  public boolean equals(Object obj){
   if(obj == null){
    return false;
   }
 
   //如果是同一个对象返回true,反之返回false
   if(this == obj){
    return true;
   }
 
   //判断是否类型相同
   if(this.getClass() != obj.getClass()){
    return false;
   }
 
   Person person = (Person)obj;
   return name.equals(person.name) && age==person.age;
  }
 }
}
Copy after login

Running results:

true
Copy after login

Result analysis:
We rewrote Person in EqualsTest2.java The equals() function: returns true when the name and age of the two Person objects are equal.
Therefore, the running result returns true.
Speaking of which, let’s talk about Java’s requirements for equals(). There are the following points:
Symmetry: If x.equals(y) returns "true", then y.equals(x) should also return "true".
Reflectivity: x.equals(x) must return "true".
Analogy: If x.equals(y) returns "true", and y.equals(z) returns "true", then z.equals(x) should also return "true".
Consistency: If x.equals(y) returns "true", as long as the contents of x and y remain unchanged, no matter how many times you repeat x.equals(y), the return will be "true".
Non-emptiness, x.equals(null), always returns "false"; x.equals (an object of a different type from x) always returns "false".
Now, let’s review the role of equals(): to determine whether two objects are equal. When we rewrite equals(), we must not change its function!


equals() 与 == 的区别是什么?
== : 它的作用是判断两个对象的地址是不是相等。即,判断两个对象是不是同一个对象。
equals() : 它的作用也是判断两个对象是否相等。但它一般有两种使用情况(前面第1部分已详细介绍过):
情况1,类没有覆盖equals()方法。则通过equals()比较该类的两个对象时,等价于通过“==”比较这两个对象。
情况2,类覆盖了equals()方法。一般,我们都覆盖equals()方法来两个对象的内容相等;若它们的内容相等,则返回true(即,认为这两个对象相等)。
下面,通过示例比较它们的区别。
代码如下:

import java.util.*;
import java.lang.Comparable;
 
/**
 * @desc equals()的测试程序。
 */
public class EqualsTest3{
 
 public static void main(String[] args) {
  // 新建2个相同内容的Person对象,
  // 再用equals比较它们是否相等
  Person p1 = new Person("eee", 100);
  Person p2 = new Person("eee", 100);
  System.out.printf("p1.equals(p2) : %s\n", p1.equals(p2));
  System.out.printf("p1==p2 : %s\n", p1==p2);
 }
 
 /**
  * @desc Person类。
  */
 private static class Person {
  int age;
  String name;
 
  public Person(String name, int age) {
   this.name = name;
   this.age = age;
  }
 
  public String toString() {
   return name + " - " +age;
  }
 
  /**
   * @desc 覆盖equals方法
   */
  @Override
  public boolean equals(Object obj){
   if(obj == null){
    return false;
   }
 
   //如果是同一个对象返回true,反之返回false
   if(this == obj){
    return true;
   }
 
   //判断是否类型相同
   if(this.getClass() != obj.getClass()){
    return false;
   }
 
   Person person = (Person)obj;
   return name.equals(person.name) && age==person.age;
  }
 }
}
Copy after login

运行结果:

p1.equals(p2) : true
p1==p2 : false
Copy after login

结果分析:
在EqualsTest3.java 中:
(1) p1.equals(p2)
这是判断p1和p2的内容是否相等。因为Person覆盖equals()方法,而这个equals()是用来判断p1和p2的内容是否相等,恰恰p1和p2的内容又相等;因此,返回true。
(2) p1==p2
这是判断p1和p2是否是同一个对象。由于它们是各自新建的两个Person对象;因此,返回false。

更多Java中判断对象是否相等的equals()方法使用教程相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template