Java 基礎 - hashCode()在什么場景下需要重寫?


總結

在自定義中,重寫hashCode()不需要加@Override. 
因為Object.hashCode()並不是abstract函數。

 

在java中,hashCode()方法的主要作用是為了配合基於散列的集合一起正常運行,這樣的散列集合包含HashSet、HashMap以及HashTable。

  • 當你的自定義類Customer,要作為散列集合(HashSet、HashMap以及HashTable)的key時,就需要重寫hashCode()方法。
  • 不然,同一個Customer對象,哪怕其屬性值發生了改變,其的hashCode始終一致。
  • 因為在自定義類Customer沒重寫hashCode()時,其默認調用本地native方法 Object.hashCode(),同一個對象始終返回同一個hashCode

以我們最常用的HashMap為例,如果Customer不重寫hashCode(), 很容易造成多個對象hash值相同,當這些對象在hashmap中當為key保存,其value會被“不經意”地覆蓋。

 

不重寫hashCode()

不重寫時,hashCode 始終一致,所以往HashMap加入元素時,會覆蓋前值

public class Solution {
    public static class Customer {
        private String username;
        private int age;

        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }

    }

    public static void main(String[] args) {
        System.out.println("打印對象Customer的hashCode值----------->");
        HashMap<Customer, Integer> map = new HashMap<>();
        Customer customer = new Customer();

        System.out.println(customer.hashCode());
        map.put(customer,1);

        customer.setAge(10);
        System.out.println(customer.hashCode());
        map.put(customer,2);

        customer.setUsername("張三");
        System.out.println(customer.hashCode());
        map.put(customer,3);

        customer.setUsername("張");
        System.out.println(customer.hashCode());
        map.put(customer,4);

        customer.setAge(8);
        System.out.println(customer.hashCode());
        map.put(customer,5);

       // size = 1
        System.out.println("HashMap size ----------->" + map.size());

    }

}    

 

輸出:

打印對象Customer的hashCode值----------->
1761291320
1761291320
1761291320
1761291320
1761291320
HashMap size ----------->1

 

 

重寫hashCode()

重寫時,hashCode隨着對象屬性的改變而改變,所以往HashMap加入元素時,不會覆蓋前值。

public class Solution {
    public static class Customer {
        private String username;
        private int age;

        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }

        public int hashCode(){
            String temp = this.username + this.age;
            return temp.hashCode();
        }
    }

    public static void main(String[] args) {
        System.out.println("打印對象Customer的hashCode值----------->");
        HashMap<Customer, Integer> map = new HashMap<>();
        Customer customer = new Customer();

        System.out.println(customer.hashCode());
        map.put(customer,1);

        customer.setAge(10);
        System.out.println(customer.hashCode());
        map.put(customer,2);

        customer.setUsername("張三");
        System.out.println(customer.hashCode());
        map.put(customer,3);

        customer.setUsername("張");
        System.out.println(customer.hashCode());
        map.put(customer,4);

        customer.setAge(8);
        System.out.println(customer.hashCode());
        map.put(customer,5);

        // size = 5
        System.out.println("HashMap size ----------->" + map.size());

    }

}

 

輸出:

打印對象Customer的hashCode值----------->
105180041
-1034385946
744669896
23403839
754968
HashMap size ----------->5

 

詳細:https://zhuanlan.zhihu.com/p/63770669


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM