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

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

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

問題描述

我正在使用許多 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>.

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

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> 編寫返回此值的方法非常簡單:

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 之外,我需要編寫幾乎相同的方法.

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>?

推薦答案

順序很重要,這就是為什么我認為我不能使用 myMap.keySet() 一個 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:

地圖的順序定義為地圖集合視圖上的迭代器返回其元素的順序.一些地圖實現,如 TreeMap 類,對它們的順序做出特定的保證;其他的,比如 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.

因此,您無需為此編寫單獨的方法.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.

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

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

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

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

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

這篇關于將 LinkedHashMap 中的所有鍵提取到列表中的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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屋-程序員
主站蜘蛛池模板: 国产精品久久久久不卡 | 国产欧美在线视频 | 97精品久久 | 中文字幕av一区 | 黄色av网站在线观看 | 国产精品美女久久久久久免费 | 伊人艹 | 国产专区视频 | 日韩视频精品在线 | 狠狠操狠狠操 | 国产精品完整版 | www.成人.com | 中文字幕高清免费日韩视频在线 | 久久久久久亚洲精品 | 手机在线观看 | 国产成人精品免费视频 | 亚洲国产日韩欧美 | 亚洲欧美精品 | 国产精品久久久久久52avav | 在线观看中文字幕dvd播放 | 国产高清一区二区三区 | 中文字幕 国产精品 | 中文字幕一区在线观看视频 | 久久97精品 | 在线看av网址 | 亚洲电影专区 | 美女视频三区 | 人和拘一级毛片c | 久久久精品一区二区三区 | 91麻豆久久久| 99在线免费观看视频 | 色免费在线视频 | 国户精品久久久久久久久久久不卡 | 亚洲女人天堂成人av在线 | 小川阿佐美pgd-606在线 | 国产精品久久久久久二区 | 久久精品一区二区视频 | 欧美日韩在线一区二区三区 | 国产成人99久久亚洲综合精品 | 综合网在线 | 亚洲综合无码一区二区 |