从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("\nKey\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[]) {
//创建哈希映射
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("\nKey\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热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短