久久久久久久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ì)象類型返回,以便我可以繼續(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>.

有什么方法可以概括我粘貼的方法以接受所有三種類型: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 類,對(duì)它們的順序做出特定的保證;其他的,比如 HashMap 類,則不需要.

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屋-程序員
主站蜘蛛池模板: 最近中文字幕在线观看 | 久久91精品| 国产又粗又长又爽 | 青青草国产精品 | 日韩综合久久 | 一本在线 | 久久国产精品免费视频 | 午夜国产在线观看 | 午夜性福利 | 色偷偷网站 | 日韩国产一区 | 国产在线视频网站 | 日韩精品视频在线播放 | 欧美视频在线观看 | 黄色大片在线免费观看 | 日韩国产精品视频 | 青青草视频 | 老司机深夜福利视频 | 日韩在线免费视频 | 日韩精品一区二区视频 | 黄色片视频在线观看 | 免费在线观看黄色片 | 午夜无遮挡 | 精品热久久| 国产成人亚洲精品自产在线 | 亚洲第一在线 | 天天综合影院 | 亚洲免费婷婷 | 国产综合视频 | 久久激情网 | 成人国产网站 | 九九色综合 | 国产欧美日韩一区二区三区 | 亚洲激情欧美激情 | 黄色国产视频 | 亚洲精品一区二三区 | 午夜性影院 | 伊人成人在线 | 亚洲精品乱码久久久久久动漫 | 久久久亚洲精品视频 | 亚洲精品区 |