首页 > 编程语言 >equals() and hashCode() methods in Java

equals() and hashCode() methods in Java

时间:2022-11-27 02:55:39浏览次数:72  
标签:return methods Object equals Geek Objects Java method

https://www.geeksforgeeks.org/equals-hashcode-methods-java/

 

Java.lang.object has two very important methods defined: public boolean equals(Object obj) and public int hashCode().

equals() method

 

In java equals() method is used to compare equality of two Objects. The equality can be compared in two ways:

  • Shallow comparison: The default implementation of equals method is defined in Java.lang.Object class which simply checks if two Object references (say x and y) refer to the same Object. i.e. It checks if x == y. Since Object class has no data members that define its state, it is also known as shallow comparison.
  • Deep Comparison: Suppose a class provides its own implementation of equals() method in order to compare the Objects of that class w.r.t state of the Objects. That means data members (i.e. fields) of Objects are to be compared with one another. Such Comparison based on data members is known as deep comparison.

Syntax :

  <iframe data-google-container-id="1" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="250" id="google_ads_iframe_/27823234/GFG_Desktop_PostContent_336x280_0" marginheight="0" marginwidth="0" scrolling="no" src="https://60c7980576ec0ec88f7bb6bf3c975f90.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="300"></iframe>
public boolean equals  (Object obj)

// This method checks if some other Object
// passed to it as an argument is equal to 
// the Object on which it is invoked.

Some principles of equals() method of Object class : If some other object is equal to a given object, then it follows these rules:

    • Reflexive : for any reference value a, a.equals(a) should return true.
    • Symmetric : for any reference values a and b, if a.equals(b) should return true then b.equals(a) must return true.
    • Transitive : for any reference values a, b, and c, if a.equals(b) returns true and b.equals(c) returns true, then a.equals(c) should return true.
    • Consistent : for any reference values a and b, multiple invocations of a.equals(b) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.

Note: For any non-null reference value a, a.equals(null) should return false.

 
// Java program to illustrate  // how hashCode() and equals() methods work import java.io.*;    class Geek  {            public String name;     public int id;                Geek(String name, int id)      {                        this.name = name;         this.id = id;     }            @Override     public boolean equals(Object obj)     {                // checking if both the object references are      // referring to the same object.     if(this == obj)             return true;                    // it checks if the argument is of the          // type Geek by comparing the classes          // of the passed argument and this object.         // if(!(obj instanceof Geek)) return false; ---> avoid.         if(obj == null || obj.getClass()!= this.getClass())             return false;                    // type casting of the argument.          Geek geek = (Geek) obj;                    // comparing the state of argument with          // the state of 'this' Object.         return (geek.name == this.name && geek.id == this.id);     }            @Override     public int hashCode()     {                    // We are returning the Geek_id          // as a hashcode value.         // we can also return some          // other calculated value or may         // be memory address of the          // Object on which it is invoked.          // it depends on how you implement          // hashCode() method.         return this.id;     }        }    //Driver code class GFG {            public static void main (String[] args)     {                // creating the Objects of Geek class.         Geek g1 = new Geek("aa", 1);         Geek g2 = new Geek("aa", 1);                    // comparing above created Objects.         if(g1.hashCode() == g2.hashCode())         {                if(g1.equals(g2))                 System.out.println("Both Objects are equal. ");             else                 System.out.println("Both Objects are not equal. ");                }         else         System.out.println("Both Objects are not equal. ");       }

Output:

Both Objects are equal.

In above example see the line:

// if(!(obj instanceof Geek)) return false;--> avoid.-->(a)

We’ve used this line instead of above line:

if(obj == null || obj.getClass()!= this.getClass()) return false; --->(y)

Here, First we are comparing the hashCode on both Objects (i.e. g1 and g2) and if same hashcode is generated by both the Objects that does not mean that they are equal as hashcode can be same for different Objects also, if they have the same id (in this case). So if get the generated hashcode values are equal for both the Objects, after that we compare the both these Objects w.r.t their state for that we override equals(Object) method within the class. And if both Objects have the same state according to the equals(Object) method then they are equal otherwise not. And it would be better w.r.t. performance if different Objects generates different hashcode value.

Reason : Reference obj can also refer to the Object of subclass of Geek. Line (b) ensures that it will return false if passed argument is an Object of subclass of class Geek. But the instanceof operator condition does not return false if it found the passed argument is a subclass of the class Geek. Read InstanceOf operator.

hashCode() method

It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing based collections like HashMap, HashSet, HashTable….etc. This method must be overridden in every class which overrides equals() method.
Syntax :

public int hashCode()

// This method returns the hash code value 
// for the object on which this method is invoked.

The general contract of hashCode is:

  • During the execution of the application, if hashCode() is invoked more than once on the same Object then it must consistently return the same Integer value, provided no information used in equals(Object) comparison on the Object is modified. It is not necessary that this Integer value to be remained same from one execution of the application to another execution of the same application.
  • If two Objects are equal, according to the equals(Object) method, then hashCode() method must produce the same Integer on each of the two Objects.
  • If two Objects are unequal, according to the equals(Object) method, It is not necessary the Integer value produced by hashCode() method on each of the two Objects will be distinct. It can be same but producing the distinct Integer on each of the two Objects is better for improving the performance of hashing based Collections like HashMap, HashTable…etc.

Note: Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.

Related link : Overriding equal in Java
Reference: JavaRanch

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

标签:return,methods,Object,equals,Geek,Objects,Java,method
From: https://www.cnblogs.com/kungfupanda/p/16928899.html

相关文章

  • enum in Java
    https://www.geeksforgeeks.org/enum-in-java/Enumerationsservethepurposeofrepresentingagroupofnamedconstantsinaprogramminglanguage.Forexample,t......
  • Generics in Java
    https://www.geeksforgeeks.org/generics-in-java/ Generics means parameterizedtypes.Theideaistoallowtype(Integer,String,…etc.,anduser-defined......
  • java发送邮件
    一邮件协议收发邮件具有与HTTP协议相同的邮件传输协议.SMTP:(SimpleMailTransferProtocol,简单邮件传输协议)发邮件协议POP3:(PostOfficeProtocolVersion3,邮局协议第......
  • Java中YYYY-MM-dd在跨年时出现的bug
    先看一张图:Bug的产生原因:日期格式化时候,把yyyy-MM-dd写成了YYYY-MM-dd Bug分析:当时间是2019-08-31时,publicclassDateTest{publicstaticvoidmain(Strin......
  • java中乐观锁CAS的实现探究——AtomicInteger
    CASCAS——compareandswap,比较并交换。是一种乐观锁的实现方式。更新操作时,在每次更新之前,都先比较一下被更新的目标T,值是不是等于读取到的旧值O,如果相等,则认为在读取......
  • JAVA网络编程TCP实现聊天功能,附在IDEA中同时运行2个或以上相同的java程序
    在IDEA中同时运行2个或以上相同的java程序在日常编写测试代码时,有时候会需要在idea上同时运行两个及以上相同的java程序,如:想运行两个CLIENTLOGIN测试聊天室效果。1.点击E......
  • java9
    Java9新特性Java9发布于2017年9月22日,带来了很多新特性,其中最主要的变化是已经实现的模块化系统。接下来我们会详细介绍Java9的新特性。Java9新特性模块系......
  • 基于java+swing的图书管理系统
    功能展示登录管理员-主界面管理员-增加书籍管理员-更新和删除书籍管理员-查找书籍管理员-查找所有书籍管理员-添加用户管理员-更新和删除用户管理员-查找......
  • Javascript(笔记53) - promise - 3 几个关键问题
    如何改变Promise的状态?1)resolve(value):如果当前是pending 就会变为resolved(或fulfilled); 2)reject(reason):如果当前是pending 就会变为rejected; 3) 抛出异常:如......
  • java中abstract详解
     Abstract(抽象)可以修饰类、方法 如果将一个类设置为abstract,则此类必须被继承使用。此类不可生成对象,必须被继承使用。 Abstract可以将子类的共性最大限度的抽取出来,放......