将 LinkedHashMap 中的所有键提取到列表中的方法

时间:2022-11-17
本文介绍了将 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模板网!

上一篇:Java - 线程“main"中的异常java.util.ConcurrentMod 下一篇:HashMap 中 NULL 键的哈希码

相关文章

最新文章