HashCode相关知识
程序员文章站
2022-03-15 20:47:31
...
1、Object的方法HashCode();
jdk文档说明
同一个对象多次调用HashCode()返回值一样,前提是对象没有改变
两个对象equals相等,hash值相等
两个对象equals不相等,hash值不同
总结:hash值相等的对象不一定相等,hash值不相等的对象一定不相等
/**
* 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}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>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.
* <li>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.
* <li>It is <em>not</em> 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.
* </ul>
* <p>
* 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
*/
2、String hash值说明
class entery{
private String name;
private String sex;
public entery(String name, String sex) {
this.name = name;
this.sex = sex;
}
public static void main(String[] args){
entery e1= new entery("李四","男");
entery e2= new entery("李四","男");
String s1=new String("李四");
String s2=new String("李四");
System.out.println(e1.equals(e2));//false
System.out.println(e1.hashCode()+"--"+e2.hashCode());//1259475182--1300109446
System.out.println(s1.equals(s2));//true
System.out.println(s1.hashCode()+"--"+s2.hashCode());//842061--842061
}
}
由于String对象重写了equals和hashCode方法,所以两个对象相等并且hash值相等
entery类由于新建了两个不同的对象,引用地址不同,所有hash不同,equals不相等,entery要想实现与String相同的效果可以重写equals和hashCode方法
3、hashMap存值,key值可以是对象,但注意对象需要重写equals和hashCode方法,否则就会出现下列情况
public class Test {
public static void main(String[] args){
entery en=new entery("张三","男");
Map map=new HashMap<Object,String>();
map.put(en,"1");
map.put(new entery("李四","男"),"2");
System.out.println(en.hashCode());
System.out.println(map.get(en));
System.out.println(map.get(new entery("李四","男")));
}
}
class entery{
private String name;
private String sex;
public entery(String name, String sex) {
this.name = name;
this.sex = sex;
}
}