将所有键从LinkedHashMap提取到列表中的方法 [英] Method to extract all keys from LinkedHashMap into a List

查看:324
本文介绍了将所有键从LinkedHashMap提取到列表中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用许多LinkedHashMap,它们是LinkedHashMap<Long, Long>LinkedHashMap<Long, Double>LinkedHashMap<Long, Integer>.

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

我的目标是找到或创建一个方法,该方法将以相同顺序返回上述LinkedHashMap<Long,...> 中所有键的List<Long>.排序很重要,这就是为什么我认为我不能使用myMap.keySet()的原因,而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;
}

但是,除了LinkedHashMap<Long, Double>LinkedHashMap<Long, Integer>,我需要编写几乎相同的方法.

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<Long, Integer>?

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

推荐答案

排序很重要,这就是为什么我认为我不能使用作为Set的myMap.keySet()的原因

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提取到列表中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆