从 Java 中的 HashMap 中检索一组 Map.Entry 元素
创建一个 HashMap 并向其中添加元素 -
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));以下是用于检索一组 Map.Entry 元素的代码段 -
Set s = hm.entrySet();
Iterator iter = s.iterator();
System.out.println("
Key\tValue");
while (iter.hasNext()) {
Map.Entry e = (Map.Entry) iter.next();
System.out.println(e.getKey() + " " + e.getValue());
}以下是一个从 HashMap 中检索一组 Map.Entry 元素的示例 -
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
// Create hash map
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));
System.out.println("Map = "+hm);
Set s = hm.entrySet();
Iterator iter = s.iterator();
System.out.println("
Key\tValue");
while (iter.hasNext()) {
Map.Entry e = (Map.Entry) iter.next();
System.out.println(e.getKey() + " " + e.getValue());
}
}
}输出
Map = {Backpack=1200, Belt=600, Wallet=700}
KeyValue
Backpack 1200
Belt 600
Wallet 700
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP