设为首页 加入收藏

TOP

Java中hashcode,equals和==(一)
2015-11-10 13:45:33 来源: 作者: 【 】 浏览:3
Tags:Java hashcode equals

hashcode方法返回该对象的哈希码值。


hashCode()方法可以用来来提高Map里面的搜索效率的,Map会根据不同的hashCode()来放在不同的位置,Map在搜索一个对象的时候先通过hashCode()找到相应的位置,然后再根据equals()方法判断这个位置上的对象与当前要插入的对象是不是同一个。若两个对象equals相等,但不在一个区间,根本没有机会进行比较,会被认为是不同的对象。


所以,Java对于eqauls方法和hashCode方法是这样规定的:


1、如果两个对象相同,那么它们的hashCode值一定要相同,也告诉我们重写equals方法,一定要重写hashCode方法;


2、如果两个对象的hashCode相同,它们并不一定相同


hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法。


注意到hashCode方法前面有个native的修饰符,这表示hashCode方法是由非java语言实现的,说明是一个本地方法,它的实现是根据本地机器相关的。具体的方法实现在外部,返回内存对象的地址。


/**
? ? * Returns a hash code value for the object. This method is
? ? * supported for the benefit of hashtables such as those provided by
? ? * java.util.Hashtable.
? ? *


? ? * The general contract of hashCode is:
? ? *


    ? ? *
  • Whenever it is invoked on the same object more than once during
    ? ? *? ? an execution of a Java application, the hashCode method
    ? ? *? ? must consistently return the same integer, provided no information
    ? ? *? ? used in 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 equals(Object)
    ? ? *? ? method, then calling the 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 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 hashtables.
    ? ? *

? ? *


? ? * As much as is reasonably practical, the hashCode method defined by
? ? * class 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
? ? * JavaTM programming language.)
? ? *
? ? * @return? a hash code value for this object.
? ? * @see? ? java.lang.Object#equals(java.lang.Object)
? ? * @see? ? java.util.Hashtable
? ? */
? ? public native int hashCode();


在java类中可以重写这两个方法,下面是String类中这两个类的实现。


/**
? ? * 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 = count;
? ? ? ? if (n == anotherString.count) {
? ? ? ? char v1[] = value;
? ? ? ? char v2[] = anotherString.value;
? ? ? ? int i = offset;
? ? ? ? int j = anotherString.offset;
? ? ? ? while (n-- != 0) {
? ? ? ? ? ? if (v1[i++] != v2[j++])
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? return true;
? ? ? ? }
? ? }
? ? return false;
? ? }


?


?


/**
? ? * Returns a hash code for this string. The hash code for a
? ? * String object is computed as
? ? *


? ? * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
? ? *

? ? * using int arithmetic, where s[i] is the
? ? * ith character of t
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Facebook 是如何构建首个跨平台 R.. 下一篇Java transient关键字使用小记

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: