HashCode和Equals区别(一)

我们首先来看一下HashCode的源码:


    /**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
     * 

* The general contract of {@code hashCode} is: *

    *
  • Whenever it is invoked on the same object more than once during * an execution of a Java application, the {@code hashCode} method * must consistently return the same integer, provided no information * used in {@code equals} comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. *
  • If two objects are equal according to the {@code equals(Object)} * method, then calling the {@code hashCode} method on each of * the two objects must produce the same integer result. *
  • It is not required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code hashCode} method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hash tables. *
*

* As much as is reasonably practical, the hashCode method defined by * class {@code Object} does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * Java™ programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.lang.System#identityHashCode */ public native int hashCode();

刨到这种地步,也差不多了,我们看一下上面的注释是啥意思?

返回对象的Hash Code值,支持此方法的好处是可以使用{@link java.util.HashMap}.提供的哈希表

每当在Java应用程序执行期间在同一个对象上多次调用该方法时,
{@code hashCode}方法必须一致地返回相同的整数,
前提是不修改对象上的{@code =}比较中使用的信息。
从应用程序的一次执行到同一应用程序的另一次执行,该整数不必保持一致。

String string = new String();
string="bbbb";
System.out.println(string.hashCode());
string="dddd";
System.out.println(string.hashCode());
HashCode结果1.png

如果根据{@code equals(Object)}方法两个对象相等,
那么在两个对象上调用{@code hashCode}方法必须产生相同的整数结果。

如果根据{@link java.lang.Object#equals(java.lang.Object)}方法两个对象不相等,
那么在两个对象上调用{@code hashCode}方法必须产生不同的整数结果,
这是不必要的。但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

在相当实际的情况下,类{@code Object}定义的hashCode方法确实为不同的对象返回不同的整数。
(这通常是通过将对象的内部地址转换为整数来实现的,
但是Java&trade并不需要这种实现技术;编程语言)。

/**
     * Indicates whether some other object is "equal to" this one.
     * 

* The {@code equals} method implements an equivalence relation * on non-null object references: *

    *
  • It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}. *
  • It is symmetric: for any non-null reference values * {@code x} and {@code y}, {@code x.equals(y)} * should return {@code true} if and only if * {@code y.equals(x)} returns {@code true}. *
  • It is transitive: for any non-null reference values * {@code x}, {@code y}, and {@code z}, if * {@code x.equals(y)} returns {@code true} and * {@code y.equals(z)} returns {@code true}, then * {@code x.equals(z)} should return {@code true}. *
  • It is consistent: for any non-null reference values * {@code x} and {@code y}, multiple invocations of * {@code x.equals(y)} consistently return {@code true} * or consistently return {@code false}, provided no * information used in {@code equals} comparisons on the * objects is modified. *
  • For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}. *
*

* The {@code equals} method for class {@code Object} implements * the most discriminating possible equivalence relation on objects; * that is, for any non-null reference values {@code x} and * {@code y}, this method returns {@code true} if and only * if {@code x} and {@code y} refer to the same object * ({@code x == y} has the value {@code true}). *

* Note that it is generally necessary to override the {@code hashCode} * method whenever this method is overridden, so as to maintain the * general contract for the {@code hashCode} method, which states * that equal objects must have equal hash codes. * * @param obj the reference object with which to compare. * @return {@code true} if this object is the same as the obj * argument; {@code false} otherwise. * @see #hashCode() * @see java.util.HashMap */ public boolean equals(Object obj) { return (this == obj); }

1、{@code =}方法在非空对象引用上实现等价关系:
2、它是自反的:对于任何非空引用值{@code x},
  {@code x.equals(x)}应该返回{@code true}。
3、它是对称的:对于任何非空的引用值{@code x}和{@code y},
  当且仅当{@code y = (x)}返回{@code true}时,
  {@code x.equals(y)}应该返回{@code true}。
  


4、 它是传递的:对于任何非空的参考值{@code x}, {@code y}, {@code z},如果{@code x.equals(y)}返回{@code true},
{@code y.equals(z)}返回{@code true},那么{@code x.equals(z)}应该返回{@code true}。

5、它是一致的:对于任何非空引用值{@code x}和{@code y},对{@code x.equals(y)}的多次调用
一致返回{@code true}或一致返回{@code false},但在对象被修改。
6、对于任何非空引用值{@code x}, {@code x.equals(null)}应该返回{@code false}。

7、{@code eauals}方法类{@codeObject}实现最歧视可能等价关系对象,也就是说,对于任何非空引用值{@code x}和
{@code y},该方法返回{@code true}当且仅当{@code x}和{@code y}引用同一个对象({@code x = = y}
{@code true})的值。

8、注意,通常需要在重写该方法时重写{@code hashCode}方法,以便维护{@code hashCode}方法的通用契约,
该契约声明相等的对象必须具有相等的散列代码。

你可能感兴趣的:(HashCode和Equals区别(一))