Java equals 和 hashcode 方法 (二)

2014-11-24 10:21:32 · 作者: · 浏览: 1
* 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);

}看代码,Object的equals方法,采用== 进行比较,只是比较对象的引用,如果引用的对象相同,那么就返回true.

看注释,Object的equals方法,具有如下特性

1.reflexive-自反性

x.equals(x) return true

2.symmetric-对称性

x.equals(y) return true

y.equals(x) return true


3.transitive-传递性

x.equals(y) return true


y.equals(z) return true


x.equals(z) return true


4.consistent-一致性

x.equals(y) return true //那么不管调用多少次,肯定都是返回true


5.与null的比较

x.equals(null) return false //对于none-null的x对象,每次必然返回false


6.于hashcode的关系

* 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.


需要注意的是,一般来说,如果重写了equals方法,都必须要重写hashcode方法,

来确保具有相同引用的对象,能够具有同样的hashcode值

好了,看到这里,我们就明白了,为什么重写了equals方法,一般来说就需要重写hashcode方法,

虽然这个不是强制性的,但是如果不能保证相同的引用对象,没有相同的hashcode,会对系统留下很大隐患


2.String类的equals方法

  * Compares this string to the specified object.  The result is {@code
  * true} if and only if the argument is not {@code null} and is a {@code
  * String} object that represents the same sequence of characters as this
  * object.
  *
  * @param  anObject
  *         The object to compare this {@code String} against
  *
  * @return  {@code true} if the given object represents a {@code String}
  *          equivalent to this string, {@code false} otherwise
  *
  * @see  #compareTo(String)
  * @see  #equalsIgnoreCase(String)
  */ 
 public boolean equals(Object anObject) { 
     if (this == anObject) { 
         return true; 
     } 
     if (anObject instanceof String) { 
         String anotherString = (String) anObject; 
         int n = value.length; 
         if (n == anotherString.value.length) { 
             char v1[] = value; 
             char v2[] = anotherString.value; 
             int i = 0; 
             while (n-- != 0) { 
                 if (v1[i] != v2[i]) 
                         return false; 
                 i++; 
             } 
             return true; 
         } 
     } 
     return false; 
 } 

   /**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String) anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v