hashMap中储存的是key=>value的映射
hashMap是不支持线程同步
hashMap是无序的,不记录插入的顺序
hashMap最多允许一条记录的key为null
hashMap中的元素实际是对象,保存的常见类型也是使用的它们的包装类
java hashMap和php数组很类似(php数组底层也是hashMap),可以支持设置类型,int和string都是支持的
举个栗子1:
// 初始化 HashMap<Integer, String> friend = new HashMap<Integer, String>(); // 添加元素 friend.put(0, "第一个朋友"); friend.put(1, "第二个朋友");
举个栗子2:
// 初始化
HashMap<String, String> friend = new HashMap<String, String>();
// 添加元素
friend.put("zero", "第一个朋友");
friend.put("one", "第二个朋友");
System.out.println(friend);
// 访问元素
System.out.println(friend.get("one"));
// 计算大小
System.out.println(friend.size());
// 循环迭代hashMap
for (String key : friend.keySet()) {
System.out.println("key:" + key + ",value:" + friend.get(key));
}