久久久久久久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屋-程序員
主站蜘蛛池模板: 久久国产婷婷国产香蕉 | 久久国产一区二区三区 | 美女视频一区二区三区 | 国产精品91视频 | 久久精品亚洲欧美日韩精品中文字幕 | 国产精彩视频在线观看 | 国产97久久 | 91高清视频在线观看 | www在线视频 | 欧美中文一区 | 成人av观看 | 精品久久久久久久久久久久久久久久久 | 伊人久久麻豆 | 欧美激情国产精品 | 中文字幕 在线观看 | 亚洲人人舔人人 | 4hu最新网址 | 国产精品国产成人国产三级 | av大全在线观看 | 狠狠操狠狠色 | 琪琪午夜伦伦电影福利片 | 91麻豆精品国产91久久久更新资源速度超快 | 操操操日日日 | 91社区在线观看播放 | 狠狠的日 | 国产做a爱片久久毛片 | 雨宫琴音一区二区在线 | 亚洲精品白浆高清久久久久久 | 亚洲国产高清高潮精品美女 | 91精品国产91久久久久久最新 | 美女拍拍拍网站 | 日韩一区二区在线播放 | 爱爱免费视频 | 国产精品国产三级国产aⅴ原创 | 精久久| 中文字幕欧美一区 | 日韩在线国产 | 国产精品亚洲视频 | 国产欧美日韩一区 | 日本一区不卡 | 精品久久久久久久久久久久久久久久久 |