久久久久久久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 排序的處理方法,對(duì)大家解決問題具有一定的參考價(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中對(duì)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 對(duì)其進(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è)簡(jiǎn)單的例子來查看排序.

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è)類不保證地圖的順序;特別是,它不保證訂單會(huì)隨著時(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 排序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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(將對(duì)象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個(gè)固定的Key對(duì)應(yīng)一個(gè)HashSet創(chuàng)建一個(gè)HashMap.出發(fā)點(diǎn))
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請(qǐng)求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 精品一区av | 色爱综合网 | 精品日韩一区 | 国产日韩精品在线 | 久草网址| 中文字幕欧美日韩 | 久久久一区二区三区四区 | 精品国产欧美一区二区三区成人 | 在线免费观看亚洲 | 在线免费av电影 | 日韩精品视频在线 | 日本久久网 | 91在线网| 亚洲人成在线播放 | 91色视频在线观看 | 亚洲综合在线一区二区 | 91精品一区 | 欧美亚洲在线视频 | 婷婷久久网 | 91国在线高清视频 | 午夜小电影 | 亚洲首页 | 在线免费观看a级片 | 久在线视频 | 日本精品一区二区三区视频 | 一区二区三区精品在线视频 | 亚洲一区二区三区在线播放 | www.国产| 国产一级特黄视频 | 一级黄色夫妻生活 | 久久久久综合 | av在线播放网站 | 午夜a v电影| 激情欧美一区二区三区中文字幕 | 国产精品视频在线观看 | 国产高潮好爽受不了了夜夜做 | 综合久久av | 一区二区三区久久久 | 国产有码 | 亚洲成av人片在线观看无码 | 精品国产乱码久久久久久牛牛 |