Java 将 LinkedHashMap 中的所有键提取到列表中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18929854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method to extract all keys from LinkedHashMap into a List
提问by user2763361
I am working with many LinkedHashMap
that are either LinkedHashMap<Long, Long>
, LinkedHashMap<Long, Double>
or LinkedHashMap<Long, Integer>
.
我有许多工作LinkedHashMap
是要么LinkedHashMap<Long, Long>
,LinkedHashMap<Long, Double>
或LinkedHashMap<Long, Integer>
。
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.
我的目标是找到或创建一个方法,该方法将以相同的顺序返回List<Long>
具有上述所有键的a 。排序很重要,这就是为什么我认为我不能使用which is a 的原因。此外,我还有许多其他方法仅接受作为输入,因此我希望所需的方法以该对象类型返回,以便我可以继续使用这些方法。LinkedHashMap<Long,...>
myMap.keySet()
Set<Long>
List<Long>
Writing a method to return this for, e.g., a LinkedHashMap<Long, Long>
is easy enough:
编写一个返回 this 的方法,例如, aLinkedHashMap<Long, Long>
很容易:
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;
}
However, then I need to write almost identical methods except for LinkedHashMap<Long, Double>
and LinkedHashMap<Long, Integer>
.
但是,除了LinkedHashMap<Long, Double>
and之外,我需要编写几乎相同的方法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>
?
有什么方法可以概括我粘贴的方法以接受所有三种类型:LinkedHashMap<Long, Long>
,LinkedHashMap<Long, Double>
或LinkedHashMap<Long, Integer>
?
采纳答案by Rohit Jain
The ordering is important which is why I don't think I can use myMap.keySet() which is a Set
排序很重要,这就是为什么我认为我不能使用 myMap.keySet() 这是一个 Set
The Map#keySet()
method for LinkedHashMap
will return the set in insertion order. Here's a quote from Map
documentation:
在Map#keySet()
对方法LinkedHashMap
将插入顺序返回集。这是Map
文档中的引用:
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.
地图的顺序定义为地图集合视图上的迭代器返回其元素的顺序。一些地图实现,如 TreeMap 类,对它们的顺序做出特定保证;其他的,比如 HashMap 类,没有。
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.
因此,您无需为此编写单独的方法。像keySet()
和这样的方法entrySet()
只会返回插入顺序中的条目。
Well, if you really want a List<Keys>
, then you can directly do:
好吧,如果你真的想要一个List<Keys>
,那么你可以直接这样做:
List<Long> keys = new ArrayList<>(target.keySet());
.. wherever you want a List. You don't need the method at all.
.. 任何你想要列表的地方。你根本不需要这个方法。
回答by prunge
LinkedHashMap
's consistent ordering applies to keys, values and entries.
LinkedHashMap
的一致顺序适用于键、值和条目。
You should be just fine doing:
你应该没问题:
ArrayList<Long> keys = new ArrayList<>(target.keySet());
If you need more assurance, take a look at the source codefor LinkedHashMap
. The important part:
如果你需要更多的保证,看看在源代码的LinkedHashMap
。重要的部分:
private class KeyIterator extends LinkedHashIterator<K> {
public K next() { return nextEntry().getKey(); }
}
private class ValueIterator extends LinkedHashIterator<V> {
public V next() { return nextEntry().value; }
}
private class EntryIterator extends LinkedHashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() { return nextEntry(); }
}
// These Overrides alter the behavior of superclass view iterator() methods
Iterator<K> newKeyIterator() { return new KeyIterator(); }
Iterator<V> newValueIterator() { return new ValueIterator(); }
Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); }
So the iterators for the keys, values and entries all come from the same source (nextEntry()
) which uses the linked list for iteration order.
因此,键、值和条目的迭代器都来自nextEntry()
使用链表作为迭代顺序的同一个源 ( )。