Java Map.Entry 接口
Map.Entry接口,可以用一个映射项工作。
由Map接口中声明的entrySet()方法返回一个包含映射条目的集。每个组元素都是一个Map.Entry对象。
通过Map.Entry的声明的方法总结如下表:
| 序号 | 方法描述 |
|---|---|
| 1 | boolean equals(Object obj) 如果obj是一个Map.Entry返回true,其键和值都等于调用对象。 |
| 2 | Object getKey( ) 返回此映射项的键。 |
| 3 | Object getValue( ) 返回此映射项的值。 |
| 4 | int hashCode( ) 返回此映射项的哈希码。 |
| 5 | Object setValue(Object v) 此映射条目v的集合. 如果v是不正确的类型,映射抛出一个ClassCastException异常值。如果v是空和映射不允许null键则抛出NullPointerException异常。一个UnsupportedOperationException被抛出如果映射不能更改。 |
实例
以下是表示Map.Entry如何使用的示例:
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("Zara", new Double(3434.34));
hm.put("Mahnaz", new Double(123.22));
hm.put("Ayan", new Double(1378.00));
hm.put("Daisy", new Double(99.22));
hm.put("Qadir", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into Zara's account
double balance = ((Double)hm.get("Zara")).doubleValue();
hm.put("Zara", new Double(balance + 1000));
System.out.println("Zara's new balance: " +
hm.get("Zara"));
}
}
以上实例编译运行结果如下:
Daisy 99.22 Qadir: -19.08 Zara: 3434.34 Ayan: 1378.0 Mahnaz: 123.22 Zara's new balance: 4434.34

国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













