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

HashMap 應(yīng)該是未排序的,但仍然根據(jù) key 排序

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

問題描述

根據(jù)這些:

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

Java 中的 HashMap 應(yīng)該是未排序的,但它正在根據(jù) Key 進(jìn)行排序.

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

我遇到了這個(gè)問題,因?yàn)槲倚枰迦胗唵螖?shù)據(jù).所以,我使用 LinkedHashMap 代替.但是我仍然很困惑為什么 HashMap 對其進(jìn)行排序.

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.

誰能解釋一下?

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

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();
        }

    }

結(jié)果:

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

我嘗試使用 JavaRandom 插入 50 個(gè)隨機(jī)數(shù),但發(fā)現(xiàn)一些數(shù)據(jù)未排序.但是,它仍然能夠?qū)Υ蠖鄶?shù)整數(shù)進(jìn)行排序.

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.

隨機(jī)結(jié)果:

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

推薦答案

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

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

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

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.

這篇關(guān)于HashMap 應(yīng)該是未排序的,但仍然根據(jù) key 排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個(gè)固定的Key對應(yīng)一個(gè)HashSet創(chuàng)建一個(gè)HashMap.出發(fā)點(diǎn))
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 久久久久久高清 | 国产精品成人一区二区三区夜夜夜 | 精品1区2区| 国产二区在线播放 | 国产午夜精品一区二区三区在线观看 | 国产欧美精品一区二区色综合朱莉 | 欧美一级毛片久久99精品蜜桃 | 99热欧美| 日本成人区| 日本精品免费 | 天堂av在线影院 | 成人欧美一区二区三区黑人孕妇 | 国产一区二区三区久久久久久久久 | 国产色网站 | 日韩精品在线观看一区二区 | 欧美精品日韩精品 | 国产一区二区在线观看视频 | 91精品国产综合久久久久久丝袜 | 91在线看 | 久久久精彩视频 | 成人免费av | 一二区成人影院电影网 | 日本精品一区 | 亚洲欧洲精品成人久久奇米网 | 中文字幕一区二区三区在线观看 | 亚洲精品久久久久久久久久吃药 | 久草在线| 在线免费看毛片 | 午夜电影一区二区 | 亚洲成人精品久久久 | 久久精品一级 | 欧美视频xxx | 91在线观看免费视频 | 久久国产精品91 | 久久久久91| 毛片在线免费播放 | 亚洲电影第1页 | 日本久久精 | 国产一区中文 | 欧美一级黄色网 | 噜久寡妇噜噜久久寡妇 |