【Java】Hashcode

定义

java.lang包下的Object类中注释了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();

从上面的注释中我们可以知道:

  1. HashCode的存在主要是为了查找的快捷性;
  2. 如果两个对象相同(equals相等),那么他们的HashCode一定相同;
  3. 如果重写了类的equals()方法,建议同时重写类的hashCode()方法,务必不要违反第 2 点;
  4. HashCode相同的两个对象并不一定相同,只能断定他们存放在散列储存结构中的相同位置。

例子

针对第 4 点,就拿常用的String类举一个例子。

String类中,hashCode()方法是这样的:

/**
 * Returns a hash code for this string. The hash code for a
 * {@code String} object is computed as
 * 
 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 * 
* using {@code int} arithmetic, where {@code s[i]} is the * ith character of the string, {@code n} is the length of * the string, and {@code ^} indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }

照着上面的HashCode计算方式,我们可以很简单地举出反例:

package com.vingyun;

public class HashCodeTest {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根

        String vingStr = "Ving";
        String mirrorStr = "WKPH";

        System.out.println("vingStr.equals(mirrorStr): " + vingStr.equals(mirrorStr));

        System.out.print("vingStr.hashCode() == mirrorStr.hashCode(): ");
        System.out.println(vingStr.hashCode() == mirrorStr.hashCode());

    }
}

【Java】Hashcode_第1张图片

用途

主要在散列储存结构中使用,例如:HashSetHashMap等。

你可能感兴趣的:(Java)