Java 如何枚举 Hashtable 的键和值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2216311/
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 enumerate the keys and values of a Hashtable?
提问by Freeman
I have a problem ; I have some data and I show it with Hashtable
for example I write :
我有个问题 ; 我有一些数据,我用Hashtable
我写的例子来展示它:
Enumeration keys;
keys=CellTraffic_v.elements();
while(keys.hasMoreElements())
outputBuffer.append(keys.nextElement()+"\n\n");
but it show me just values how can i show values and keys together? for example this
但它只向我显示值,我如何将值和键一起显示?例如这个
if my key be "A" and my value be "B" show me this :
如果我的键是“A”而我的值是“B”,请告诉我:
A B
Thanks ...
谢谢 ...
采纳答案by crunchdog
You have the key right? Use the key to get the value out of the map, and you have all the mappings. For example in Java with String as type for key:
你有钥匙吗?使用键从映射中获取值,您就拥有了所有映射。例如在 Java 中使用 String 作为键的类型:
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
.
.
回答by Lachlan Roche
entrySet() returns an enumeration of the values in the Hashtable.
keySet() returns an enumeration of the keys in the Hashtable.
entrySet() returns the entries (key and value) as a Set
entrySet() 返回 Hashtable 中值的枚举。
keySet() 返回哈希表中键的枚举。
entrySet() 将条目(键和值)作为集合返回
for( Iterator iter=hash.keySet().iterator(); iter.hasNext(); ) {
String key = (String) iter.next();
String value = (String) hash.get( key );
}
for( Iteration iter=hash.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
}
or using generics, in which case your hash is a HashMap<String,String>
或使用泛型,在这种情况下,您的哈希是 HashMap<String,String>
for( String key : hash.keySet() ) {
String value = hash.get( key );
}
for( Map.Entry entry : hash.entrySet() ) {
String key = entry.getKey();
String value = entry.getValue();
}
回答by T.J. Crowder
Hashtable
implements Map
. The Map.entrySet
function returns a collection (Set
) of Map.Entry
instances, which have getKey
and getValue
methods.
Hashtable
实施Map
. 该Map.entrySet
函数返回实例的集合 ( Set
) Map.Entry
,这些实例具有getKey
和getValue
方法。
So:
所以:
Iterator<Map.Entry> it;
Map.Entry entry;
it = yourTable.entrySet().iterator();
while (it.hasNext()) {
entry = it.next();
System.out.println(
entry.getKey().toString() + " " +
entry.getValue().toString());
}
If you know the types of the entries in the Hashtable, you can use templates to eliminate the toString
calls above. For instance, entry
could be declared Map.Entry<String,String>
if your Hashtable is declared Hashtable<String,String>
.
如果您知道 Hashtable 中条目的类型,则可以使用模板来消除上述toString
调用。例如,如果您的 Hashtable 已声明,则entry
可以Map.Entry<String,String>
声明Hashtable<String,String>
。
If you can combine templates with generics, it's downright short:
如果您可以将模板与泛型结合起来,那就太短了:
for (Map.Entry<String,String> entry : yourTable.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
That assumes yourTable
is a Hashtable<String,String>
. Just goes to show how far Java has come in the last few years, largely without losing its essential Java-ness.
假设yourTable
是一个Hashtable<String,String>
. 只是为了展示 Java 在过去几年中走了多远,基本上没有失去其基本的 Java 特性。
Slightly OT: If you don't need the synchronization, use HashMap
instead of Hashtable
. If you do, use a ConcurrentHashMap
(thanks, akappa!).
稍微 OT:如果您不需要同步,请使用HashMap
代替Hashtable
。如果这样做,请使用ConcurrentHashMap
(谢谢,akappa!)。