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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)