Java中的equals()和hashCode()方法

JAVAobject定义了两个非常重要的方法:public boolean equals(object obj)和public int hashCode()。

null

equals()方法

在java中,equals()方法用于比较两个对象的相等性。这种平等可以通过两种方式进行比较:

  • 浅显比较: equals方法的默认实现是在Java中定义的。对象类,它只检查两个对象引用(比如x和y)是否引用同一个对象。i、 e.它检查x==y。由于对象类没有定义其状态的数据成员,所以它也被称为浅层比较。
  • 深度比较: 假设一个类提供了自己的equals()方法实现,以便比较该类的对象和对象的状态。这意味着对象的数据成员(即字段)要相互比较。这种基于数据成员的比较被称为深度比较。

语法:

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.

对象类的equals()方法的一些原则: 如果其他对象与给定对象相等,则遵循以下规则:

  • 自反的: 对于任何参考值a,a.equals(a)应返回true。
  • 对称的: 对于任何参考值a和b,如果a.equals(b)应返回true,则b.equals(a)必须返回true。
  • 可传递的: 对于任何参考值a、b和c,如果a.equals(b)返回true,b.equals(c)返回true,那么a.equals(c)应该返回true。
  • 一致的: 对于任何参考值a和b,如果没有修改对象上equals比较中使用的信息,多次调用a.equals(b)都会一致返回true或false。

    注: 对于任何非null的引用值a,a.equals(null)应该返回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. " );
}
}


输出:

Both Objects are equal.

在上面的示例中,请参见以下行:

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

我们使用了这一行而不是上面这一行:

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

这里,我们首先比较两个对象(即g1和g2)上的哈希代码,如果两个对象生成相同的哈希代码,这并不意味着它们相等,因为如果它们具有相同的id(在本例中),不同对象的哈希代码也可以相同。因此,如果get生成的hashcode值对于这两个对象都相等,然后我们比较这两个对象的w.r.t状态,我们在类中重写equals(Object)方法。根据equals(Object)方法,如果两个对象具有相同的状态,那么它们是相等的,否则不是。如果不同的对象生成不同的hashcode值,那么w.r.t.性能会更好。

原因: 参考 obj 也可以指Geek子类的对象。第(b)行确保,如果传递的参数是Geek类的子类的对象,它将返回false。但是 运算符 如果运算符条件发现传递的参数是Geek类的子类,则不会返回false。阅读 瞬间 操作人员

hashCode()方法

它以整数形式返回哈希代码值。Hashcode值主要用于HashMap、HashSet、HashTable等基于哈希的集合…。这个方法必须在每个重写equals()方法的类中被重写。 语法:

public int hashCode()

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

hashCode的总合同是:

  • 在应用程序执行期间,如果在同一个对象上多次调用hashCode(),那么它必须一致地返回相同的整数值,前提是在应用程序中没有使用任何信息 相等(对象) 修改对象上的比较。从应用程序的一次执行到同一应用程序的另一次执行,该整数值不必保持不变。
  • 如果两个物体相等,根据 相等(对象) 方法,则hashCode()方法必须在两个对象中的每个对象上生成相同的整数。
  • 如果两个物体不相等,根据 相等(对象) 方法,则hashCode()方法在两个对象上生成的整数值不必是不同的。它可以是相同的,但在两个对象中的每一个上生成不同的整数对于提高基于哈希的集合(如HashMap、HashTable等)的性能更好。

注意:只要相等,相等的对象必须产生相同的哈希代码,但是不相等的对象不需要产生不同的哈希代码。

相关链接: 在Java中重写equal 参考: 爪哇牧场

本文由 尼茨海伦德拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享