在 Java 中获取映射(HashMap)的枚举(用于键)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2048131/
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
Get an Enumeration (for the Keys) of a Map (HashMap) in Java?
提问by tim
As far as I understand this, it seems that there is not a direct way of getting an Enumeration
directly for the Keys of a HashMap
. I can only get a keySet()
. From that Set
, I can get an Iterator
but an Iterator
seems to be something different than an Enumeration
.
据我了解,似乎没有直接获取 aEnumeration
的键的直接方法HashMap
。我只能得到一个keySet()
. 从中Set
,我可以得到 anIterator
但 anIterator
似乎与 an 不同Enumeration
。
What is the best and most performant way to directly get an Enumeration
from the Keys of a HashMap
?
直接Enumeration
从 a 的键中获取 an 的最佳和最高效的方法是HashMap
什么?
Background: I am implementing my own ResourceBundle(=>getKeys()
Method), and I have to provide/implement a method that returns the Enumeration of all Keys. But my implementation is based on a HashMap
so I need to somehow figure out how to best convert betweens these two "Iterator/Enumerator" techniques.
背景:我正在实现我自己的ResourceBundle(=>getKeys()
方法),并且我必须提供/实现一个返回所有键的枚举的方法。但是我的实现基于 aHashMap
所以我需要以某种方式弄清楚如何最好地在这两种“迭代器/枚举器”技术之间进行转换。
采纳答案by Bozho
Apache commons-collectionshave an adapter that makes the Iterator
available for use like an Enumeration
. Take a look at IteratorEnumeration.
Apache commons-collections有一个适配器,可以Iterator
像Enumeration
. 看看IteratorEnumeration。
Adapter to make an Iterator instance appear to be an Enumeration instances
使 Iterator 实例看起来像 Enumeration 实例的适配器
So in short you do the following:
简而言之,您执行以下操作:
Enumeration enumeration = new IteratorEnumeration(hashMap.keySet().iterator());
Alternatively, if you (for some reason) don't want to include commons-collections, you can implement this adapter yourself. It is easy - just make an implementation of Enumeration
, pass the Iterator
in a constructor, and whenever hasMoreElements()
and nextElement()
are called, you call the hasNext()
and next()
on the underlying Iterator
.
或者,如果您(出于某种原因)不想包含 commons-collections,您可以自己实现此适配器。这很简单 - 只需实现Enumeration
,Iterator
在构造函数中传递,并且无论何时hasMoreElements()
和nextElement()
被调用,您都在底层调用hasNext()
和。next()
Iterator
Use this if you are forced to use Enumeration
by some API contract (as I assume the case is). Otherwise use Iterator
- it is the recommended option.
如果您被迫使用Enumeration
某些 API 合同(如我所假设的情况),请使用此选项。否则使用Iterator
- 这是推荐的选项。
回答by sateesh
I think you can use the method enumerationfrom java.util.Collectionsclass to achieve what you want.
我想你可以使用的方法枚举从java.util.Collections中的类来实现你想要什么。
The API doc of the method enumerate has this to say:
方法 enumerate 的 API 文档有这样的说法:
public static Enumeration enumeration(Collection c)
Returns an enumeration over the specified collection. This provides interoperability with legacy APIs that require an enumeration as input.
public static Enumeration enumeration(Collection c)
返回指定集合的枚举。这提供了与需要枚举作为输入的遗留 API 的互操作性。
For example, the below code snippet gets an instance of Enumeration from the keyset of HashMap
例如,下面的代码片段从 HashMap 的键集中获取一个 Enumeration 实例
final Map <String,Integer> test = new HashMap<String,Integer>();
test.put("one",1);
test.put("two",2);
test.put("three",3);
final Enumeration<String> strEnum = Collections.enumeration(test.keySet());
while(strEnum.hasMoreElements()) {
System.out.println(strEnum.nextElement());
}
and resulting the output is:
one
two
three
和产生的输出是:
一个
2
3
回答by richa_v
You can write a adapter to adapt to Enumeration.
您可以编写一个适配器来适应枚举。
public class MyEnumeration implements Enumeration {
private Iterator iterator;
public MyEnumeration(Iterator iterator){
this.iterator = iterator;
}
public MyEnumeration(Map map) {
iterator = map.keySet().iterator();
}
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public Object nextElement() {
return iterator.next();
}
}
And then you can use this custom enumeration :)
然后你可以使用这个自定义枚举:)