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

HashMap 應該是未排序的,但仍然根據 key 排序

HashMap should be unsorted but still sorts according to key(HashMap 應該是未排序的,但仍然根據 key 排序)
本文介紹了HashMap 應該是未排序的,但仍然根據 key 排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

根據這些:

  1. http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
  2. HashMap、LinkedHashMap和TreeMap的區別
  3. java初學者:如何在hashmaps中對key進行排序?

Java 中的 HashMap 應該是未排序的,但它正在根據 Key 進行排序.

The HashMap in Java should be unsorted but it is being sorted with respect to Key.

我遇到了這個問題,因為我需要插入訂單數據.所以,我使用 LinkedHashMap 代替.但是我仍然很困惑為什么 HashMap 對其進行排序.

I experienced this as a problem because I needed inserted-order data. So, I used LinkedHashMap instead. But still I am confused why the HashMap sorted it.

誰能解釋一下?

我做了一個簡單的例子來查看排序.

I did a simple example to view the sort.

public static void main(String[] args) {

        HashMap<Integer, String> newHashMap = new HashMap<Integer, String>();
        newHashMap.put(2, "First");
        newHashMap.put(0, "Second");
        newHashMap.put(3, "Third");
        newHashMap.put(1, "Fourth");

        Iterator<Entry<Integer, String>> iterator = newHashMap.entrySet()
                .iterator();
        while (iterator.hasNext()) {

            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("Key: " + entry.getKey());
            System.out.println("Value: " + entry.getValue());
            iterator.remove();
        }

    }

結果:

Key: 0
Value: Second
Key: 1
Value: Fourth
Key: 2
Value: First
Key: 3
Value: Third

我嘗試使用 JavaRandom 插入 50 個隨機數,但發現一些數據未排序.但是,它仍然能夠對大多數整數進行排序.

I tried to insert 50 random numbers using Random of Java and I found some data unsorted. But, it still manages to sort most of the integers.

隨機結果:

...
Key: 36
Value: random
Key: 43
Value: random
Key: 47
Value: random
Key: 44
Value: random
Key: 45
Value: random
...

推薦答案

這是巧合(不是真的,而是與哈希算法有關).

It's a coincidence (not really, rather it has to do with the hashing algorithm).

嘗試添加

newHashMap.put(-5, "Fifth");

最后一次.

輸出將是

Key: 0
Value: Second
Key: 1
Value: Fourth
Key: 2
Value: First
Key: 3
Value: Third
Key: -5
Value: Fifth

javadoc 具體說

The javadoc specifically says

這個類不保證地圖的順序;特別是,它不保證訂單會隨著時間的推移保持不變.

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

這篇關于HashMap 應該是未排序的,但仍然根據 key 排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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屋-程序員
主站蜘蛛池模板: 在线高清免费观看视频 | 成人精品在线视频 | 久久人人网 | 国产精品久久久久久久久免费相片 | 欧美日韩综合 | xx性欧美肥妇精品久久久久久 | 久久久www成人免费无遮挡大片 | 99精彩视频 | 三级黄色片在线观看 | 亚洲人成人一区二区在线观看 | 久久91av | 欧美成人精品一区二区男人看 | 国产欧美一区二区三区久久手机版 | 日本成人片在线观看 | 中文字幕av一区二区三区 | 成人免费看片又大又黄 | 日韩一区精品 | 国产视频一区二区在线观看 | 狠狠av| 亚洲性视频 | 午夜一区 | 成人影院在线 | 亚洲欧美精品在线观看 | 日韩爱爱网 | 蜜桃视频在线观看免费视频网站www | 午夜视频网站 | 97精品超碰一区二区三区 | 欧美国产日本一区 | 久在线精品视频 | 看亚洲a级一级毛片 | 人人人人爽| 一区二区在线免费观看 | 狠狠操天天干 | 亚洲天堂久久新 | 国产一区二区在线免费观看 | 日韩爱爱网 | 特级特黄特色的免费大片 | 国产精品av久久久久久毛片 | a国产视频 | 国产九九九 | 中文字幕日本一区二区 |