用Java创建HashMap
要创建HashMap,请使用HashMap映射和新的-
HashMap hm = new HashMap();
现在,设置元素-
hm.put("Finance", new Double(999.87));
hm.put("Operations", new Double(298.64));
hm.put("Marketing", new Double(39.56));现在使用以下代码显示元素-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
//创建一个哈希映射
HashMap hm = new HashMap();
//将元素放入映射
hm.put("Finance", new Double(999.87));
hm.put("Operations", new Double(298.64));
hm.put("Marketing", new Double(39.56));
//获取一组条目
Set set = hm.entrySet();
//获取一个迭代器
Iterator i = set.iterator();
//显示元素
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
}
}输出结果
Finance: 999.87 Operations: 298.64 Marketing: 39.56
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短