HashCode相關知識

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&trade; 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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章