如何获取 LinkedHashMap 的 keyIterator?

发布于 2024-07-23 05:26:20 字数 78 浏览 6 评论 0原文

通过查看Sun的LinkedHashMaps的源代码,我发现有一个名为KeyIterator的私有类,我想使用它。 我怎样才能获得访问权限?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-07-30 05:26:20

你可以通过调用来获得它

myMap.keySet().iterator();

,你甚至不需要知道它的存在; 它只是实现的产物。 据您所知,他们可能使用会飞的猴子来迭代密钥; 只要它们按照规范进行迭代,它们如何做并不重要。

顺便说一句,您是否知道 HashMap 有一个名为 KeyIterator 的私有类(ConcurrentHashMapConcurrentSkipListMap 也是如此) EnumMapIdentityHashMapTreeMapWeakHashMap)?
这会对您迭代 HashMap 的键的方式产生影响吗?


编辑:在回复您的评论时,请注意,如果您尝试迭代Map中的所有键值对,则有这是比迭代键并为每个键调用 get 更好的方法。 entrySet()方法获取所有键值对的 Set,然后您可以对其进行迭代。 因此,您不应编写:

for (Key key : myMap.keySet()) {
    Value val = myMap.get(key);
    ...
}

您应该编写:

for (Map.Entry<Key, Value> entry : myMap.entrySet()) {
    doSomethingWithKey(entry.getKey());
    doSomethingWithValue(entry.getValue());
    ...
}

您还可以使用 values() 如果你愿意的话。

请注意,由于 keySetentrySetvalues 是在 Map 接口,它们适用于任何 Map,而不是只是LinkedHashMap

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?


Edit: In reponse to your comment, be aware that if you are trying to iterate over all key-value pairs in 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:

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.

Note that since keySet, entrySet, and values are defined in the Map interface, they will work for any Map, not just LinkedHashMap.

南风起 2024-07-30 05:26:20

这是一个私有类,所以你不能直接使用它。

  private class KeyIterator extends LinkedHashIterator<K> {

当您使用普通迭代器时,会返回它的一个实例。

myMap.keySet().iterator()

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.

myMap.keySet().iterator()
昵称有卵用 2024-07-30 05:26:20

您不应该使用定义为 LinkedHashMap 内部实现的一部分的任何内容(即在源代码中但未在 API 中定义)。 如果下一个版本中内部实现发生变化会发生什么? 使用它的所有代码都会中断。

您应该编写 API 并执行就像是

myMap.keySet().iterator()

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.

You should code to the API and do something like

myMap.keySet().iterator()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文