简体   繁体   English

如何枚举哈希表的键和值?

[英]How do I enumerate the keys and values of a Hashtable?

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 ... 谢谢 ...

Hashtable implements Map . Hashtable实现Map The Map.entrySet function returns a collection ( Set ) of Map.Entry instances, which have getKey and getValue methods. Map.entrySet函数返回具有getKeygetValue方法的Map.Entry实例的集合( Set )。

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. 如果您知道哈希表中条目的类型,则可以使用模板消除上面的toString调用。 For instance, entry could be declared Map.Entry<String,String> if your Hashtable is declared Hashtable<String,String> . 例如,如果您的哈希表被声明为Hashtable<String,String> ,则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> . 假设yourTableHashtable<String,String> Just goes to show how far Java has come in the last few years, largely without losing its essential Java-ness. 只是说明了最近几年Java的发展,在很大程度上没有失去其本质的Java风格。

Slightly OT: If you don't need the synchronization, use HashMap instead of Hashtable . OT:如果不需要同步,请使用HashMap而不是Hashtable If you do, use a ConcurrentHashMap (thanks, akappa!). 如果这样做,请使用ConcurrentHashMap (感谢akappa!)。

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));
}

.

entrySet() returns an enumeration of the values in the Hashtable. entrySet()返回Hashtable中的值的枚举。
keySet() returns an enumeration of the keys in the Hashtable. keySet()返回Hashtable中的键的枚举。
entrySet() returns the entries (key and value) as a Set entrySet()以Set形式返回条目(键和值)

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM