Java 如何获得 LinkedHashMap 的 keyIterator?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/978200/
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
How do I get a keyIterator for a LinkedHashMap?
提问by soldier.moth
By looking at the source code for LinkedHashMaps from Sun, I see that there is a private class called KeyIterator, I would like to use this. How can I gain access?
通过查看 Sun 的 LinkedHashMaps 的源代码,我看到有一个名为 KeyIterator 的私有类,我想使用它。我如何获得访问权限?
采纳答案by Michael Myers
You get it by calling
你通过调用它
myMap.keySet().iterator();
You shouldn't even need to know it exists; it's just an artifact of the implementation. For all you know, they could be using flying monkeys to iterate the keys; as long as they're iterated according to the spec, it doesn't matter how they do it.
你甚至不需要知道它的存在;它只是实现的产物。据您所知,他们可能会使用飞猴来迭代密钥;只要他们根据规范进行迭代,他们如何做并不重要。
By the way, did you know that HashMap
has a private class called KeyIterator
(as do ConcurrentHashMap
, ConcurrentSkipListMap
, EnumMap
, IdentityHashMap
, TreeMap
, and WeakHashMap
)?
Does that make a difference in how you iterate through the keys of a HashMap
?
顺便说一下,你知道HashMap
有一个私有类叫做KeyIterator
(如ConcurrentHashMap
, ConcurrentSkipListMap
, EnumMap
, IdentityHashMap
, TreeMap
, and WeakHashMap
)?
这对您遍历 a 的键的方式有影响HashMap
吗?
Edit:In reponse to your comment, be aware that if you are trying to iterate over all key-value pairsin a Map
, there is a better way than iterating over the keys and calling get
for each. The entrySet()
method gets a Set
of all key-value pairs which you can then iterate over. So instead of writing:
编辑:在效应初探您的意见,要知道,如果你在所有试图迭代键-值对的Map
,还有比遍历键和调用一个更好的方式get
为每个。该entrySet()
方法获取Set
所有键值对,然后您可以对其进行迭代。所以而不是写:
for (Key key : myMap.keySet()) {
Value val = myMap.get(key);
...
}
you should write:
你应该写:
for (Map.Entry<Key, Value> entry : myMap.entrySet()) {
doSomethingWithKey(entry.getKey());
doSomethingWithValue(entry.getValue());
...
}
You could also iterate over the values with values()
if you want.
如果需要,您还可以迭代这些值values()
。
Note that since keySet
, entrySet
, and values
are defined in the Map
interface, they will work for any Map
, not just LinkedHashMap
.
请注意,由于keySet
,entrySet
和values
是在Map
接口中定义的,因此它们适用于任何Map
,而不仅仅是LinkedHashMap
。
回答by Rob Hruska
You shouldn't use anything that is defined as part of the internal implementation of LinkedHashMap (i.e. in the source code but not defined in the API). What happens if the internal implementation changes in the next release? All of your code using it will break.
您不应使用在 LinkedHashMap 的内部实现中定义的任何内容(即在源代码中但未在 API 中定义)。如果在下一个版本中内部实现发生变化,会发生什么?您使用它的所有代码都会中断。
You should code to the APIand do something like
您应该编写API并执行类似操作
myMap.keySet().iterator()
回答by Mike Pone
It's a private class, so you can't directly use it.
这是一个私有类,所以你不能直接使用它。
private class KeyIterator extends LinkedHashIterator<K> {
An instance of it is returned when you use the normal Iterator.
当您使用普通的 Iterator 时,会返回它的一个实例。
myMap.keySet().iterator()