久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 HashMap 調整大小以防發生碰撞或最壞的情

Why HashMap resize In case of collision or worst case(為什么 HashMap 調整大小以防發生碰撞或最壞的情況)
本文介紹了為什么 HashMap 調整大小以防發生碰撞或最壞的情況的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我只針對 1.7 之前的 java 版本提出這個問題.我正在使用反射來找出 HashMap 的當前容量.在下面的程序中,將 12 個唯一的人放入一個 HashMap 桶中(使用相同的哈希碼).然后我將第 13 個獨特的人放在相同或不同的存儲桶上(使用相同或不同的哈希碼).在這兩種情況下,添加第 13 個元素后,HashMap 都將大小調整為 32 個桶.我知道由于負載因子 0.75 和初始容量 16 HashMap 調整到其第 13 個元素的兩倍.但是仍然有可用的空桶,并且只有 2 個桶用于這第 13 個元素.

I am asking this question with respect to java version till 1.7 only. I am using reflection to find out current capacity of HashMap. In below program is putting 12 unique person into a single bucket of HashMap (using same hashcode). Then i am putting 13th unique person on same or different bucket(using same or different hashcodes). In both cases after adding this 13th element, HashMap resizes to 32 buckets. I understand that due to load factor .75 and initial capacity 16 HashMap resizes to its double with 13th element. But there are still empty buckets available and only 2 buckets are used for these 13th elements.

我的問題是:

  1. 我的理解是否正確.難道我沒有犯任何錯誤.這是 HashMap 的預期行為嗎?

  1. Is my understanding correct. Am I not making any mistake. Is this the expected behavior of HashMap?

如果所有這些都是正確的,那么即使有 12 或 11 個空閑桶,為什么在這種情況下需要將 HashMap 與第 13 個元素加倍.調整 HashMap 的大小不是額外的開銷或成本嗎?在這種情況下需要將 HashMap 加倍嗎?而根據 hashcode 可以將 13th 放入任何可用的桶中?

If all this is correct then even though there are 12 or 11 free buckets why the need to double the HashMap with 13th element in this case. Isn't it extra overhead or costly to resize the HashMap? What is the need to double the HashMap in this case While 13th can be put in any available bucket according to hashcode?

.

public class HashMapTest {
    public static void main(String[] args)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        HashMap<Person, String> hm = new HashMap<Person, String>();
        for (int i = 1; i <= 12; i++) {
            // 12 Entry in same bucket(linkedlist)
            hm.put(new Person(), "1");
        }
        System.out.println("Number of Buckets in HashMap : " + bucketCount(hm));
        System.out.println("Number of Entry in HashMap :  " + hm.size());
        System.out.println("**********************************");
        // 13th element in different bucket
        hm.put(new Person(2), "2");
        System.out.println("Number of Buckets in HashMap : " + bucketCount(hm));
        System.out.println("Number of Entry in HashMap :  " + hm.size());
    }

    public static int bucketCount(HashMap<Person, String> h)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Field tableField = HashMap.class.getDeclaredField("table");
        tableField.setAccessible(true);
        Object[] table = (Object[]) tableField.get(h);
        return table == null ? 0 : table.length;
    }
}

class Person {
    int age = 0;

    Person() {
    }

    Person(int a) {
        age = a;
    }

    @Override
    public boolean equals(Object obj) {
        return false;
    }

    @Override
    public int hashCode() {
        if (age != 0) {
            return 1;
        } else {
            return age;
        }
    }
}

輸出

Number of Buckets in HashMap : 16
Number of Entry in HashMap :  12
**********************************
Number of Buckets in HashMap : 32
Number of Entry in HashMap :  13

推薦答案

  1. 是的,這是預期的行為.
  2. HashMap 并不關心使用了多少桶.它只知道已經達到負載因子,并且因此發生碰撞的概率變得太大,因此應該調整地圖的大小.盡管已經發生了許多碰撞,但調整地圖大小實際上可以解決這個問題.不是你的情況,因為你故意選擇了相同的 hashCode,但在更現實的情況下,hashCodes 應該有更好的分布.如果您故意選擇錯誤的 hashCodes,HashMap 無法做任何事情來提高自己的效率,并且增加復雜性來處理極端情況是沒有意義的,這種情況永遠不會發生,而且 HashMap 無論如何也無法修復.

這篇關于為什么 HashMap 調整大小以防發生碰撞或最壞的情況的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 91免费版在线观看 | 久久成人高清视频 | 98成人网 | 久久久久亚洲精品 | 欧美高清视频一区 | 视频国产一区 | 色综合久久88色综合天天 | 狠狠爱免费视频 | 超碰在线人 | 亚洲三级视频 | 欧美一区免费 | 高清欧美性猛交xxxx黑人猛交 | 欧美视频三区 | 日韩一区二区三区精品 | 亚洲小视频 | 一区视频在线 | 免费一看一级毛片 | 国产激情一区二区三区 | 91www在线观看 | 中文字幕视频在线观看 | 精品一区二区三区在线观看国产 | 中文字幕在线观看日韩 | 在线免费国产 | 久久伊人精品 | 日韩欧美一区二区三区免费观看 | av官网在线 | 国产三级大片 | 亚洲一区免费视频 | 欧美日韩一区在线 | 久久国内 | 成人午夜网 | 国产一区二区 | 精品久久国产 | 欧美一区二区三区在线观看 | 日韩电影在线一区 | 亚洲成人精品久久久 | 亚洲a人 | 欧美99| 亚洲色图婷婷 | 国产激情一区二区三区 | 成人小视频在线观看 |