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

將 LinkedHashMap 中的所有鍵提取到列表中的方法

Method to extract all keys from LinkedHashMap into a List(將 LinkedHashMap 中的所有鍵提取到列表中的方法)
本文介紹了將 LinkedHashMap 中的所有鍵提取到列表中的方法的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在使用許多 LinkedHashMap,它們是 LinkedHashMap<Long, Long>LinkedHashMap<Long, Double>LinkedHashMap.

I am working with many LinkedHashMap that are either LinkedHashMap<Long, Long>, LinkedHashMap<Long, Double> or LinkedHashMap<Long, Integer>.

我的目標(biāo)是找到或創(chuàng)建一個(gè)方法,該方法將返回一個(gè) List<Long>,其中包含上述 LinkedHashMap<Long,...> 中的所有鍵順序相同.排序很重要,這就是為什么我認(rèn)為我不能使用 myMap.keySet(),它是一個(gè) Set<Long>.此外,我還有許多其他方法只接受 List<Long> 作為輸入,因此我希望所需的方法以該對(duì)象類(lèi)型返回,以便我可以繼續(xù)使用這些方法.

My objective is to find or create a method that will return a List<Long> with all the keys in the above LinkedHashMap<Long,...> in the same order. The ordering is important which is why I don't think I can use myMap.keySet() which is a Set<Long>. Also, I have many other methods that accept only List<Long> as the input so I would like the desired method to return in that object type so I can continue to use these methods.

LinkedHashMap<Long, Long> 編寫(xiě)返回此值的方法非常簡(jiǎn)單:

Writing a method to return this for, e.g., a LinkedHashMap<Long, Long> is easy enough:

private static List<Long> getLongKeys(LinkedHashMap<Long, Long> target) {
    List<Long> keys = new ArrayList<Long>();

    for(Map.Entry<Long, Long> t : target.entrySet()) {
        keys.add(t.getKey());
    }
    return keys;
}

但是,除了 LinkedHashMapLinkedHashMap 之外,我需要編寫(xiě)幾乎相同的方法.

However, then I need to write almost identical methods except for LinkedHashMap<Long, Double> and LinkedHashMap<Long, Integer>.

有什么方法可以概括我粘貼的方法以接受所有三種類(lèi)型:LinkedHashMap<Long, Long>, LinkedHashMap<Long, Double>LinkedHashMap?

Is there any way I can generalize the method that I pasted to accept all three types: LinkedHashMap<Long, Long>, LinkedHashMap<Long, Double> or LinkedHashMap<Long, Integer>?

推薦答案

順序很重要,這就是為什么我認(rèn)為我不能使用 myMap.keySet() 一個(gè) Set

The ordering is important which is why I don't think I can use myMap.keySet() which is a Set

LinkedHashMapMap#keySet() 方法將按插入順序返回集合.這是 Map 文檔:

The Map#keySet() method for LinkedHashMap will return the set in insertion order. Here's a quote from Map documentation:

地圖的順序定義為地圖集合視圖上的迭代器返回其元素的順序.一些地圖實(shí)現(xiàn),如 TreeMap 類(lèi),對(duì)它們的順序做出特定的保證;其他的,比如 HashMap 類(lèi),則不需要.

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

因此,您無(wú)需為此編寫(xiě)單獨(dú)的方法.keySet()entrySet() 等方法將僅返回插入順序中的條目.

So, you don't need to write a separate method for that. Methods like keySet() and entrySet() will return the entries in the insertion order only.

好吧,如果你真的想要一個(gè)List<Keys>,那么你可以直接這樣做:

Well, if you really want a List<Keys>, then you can directly do:

List<Long> keys = new ArrayList<>(target.keySet());

.. 任何你想要一個(gè)列表的地方.你根本不需要這個(gè)方法.

.. wherever you want a List. You don't need the method at all.

這篇關(guān)于將 LinkedHashMap 中的所有鍵提取到列表中的方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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:無(wú)法寫(xiě)入請(qǐng)求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 岛国av一区二区 | 国产精品久久久久久福利一牛影视 | 超级黄色一级片 | 亚洲日本中文 | 国产精品精品久久久 | 玖玖国产精品视频 | 久久99精品视频 | 国产a视频 | 中文字幕亚洲一区二区三区 | 国产一区三区视频 | 亚洲性人人天天夜夜摸 | 九九热在线视频观看这里只有精品 | 精品欧美乱码久久久久久1区2区 | 久久亚洲欧美日韩精品专区 | 色综合av | 亚洲精品女人久久久 | 亚洲一区二区视频 | 一二三在线视频 | 浴室洗澡偷拍一区二区 | 国产免费一区二区三区 | 给我免费的视频在线观看 | 999久久久久久久 | 久久51 | 91在线看 | 欧美精品在线一区二区三区 | 日韩成人影院在线观看 | 久久av一区 | 日韩成人在线播放 | 91在线看 | 国产片侵犯亲女视频播放 | 国产精品精品久久久 | 99国产精品99久久久久久 | 日韩靠逼 | 一区二区在线 | 免费九九视频 | 久久99久久98精品免观看软件 | 男人天堂久久 | 久久综合国产 | 日韩欧美视频在线 | 青青草华人在线视频 | 日本不卡免费新一二三区 |