1 Map
- 框架图
![在这里插入图片描述](https://img-blog.csdnimg.cn/6e50d80c51b44419a8da1ee7ee1c0bd8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y2X5YyX5pyJ5Lic6KW_,size_20,color_FF34FF,t_70,g_se,x_16#pic_center)
- Map存放的是一个键值对(key-value)
- 当key相同时,value会被替换
- 通过key可以找到Value
1.1 Map常用方法
Map map = new HashMap();
map.put("Curry",null);
map.put("Curry", "MVP");
map.put("Stephen", "MVP");
map.put("James", null);
map.put(null, "James");
System.out.println("map=" + map);
map.remove(null);
System.out.println("map=" + map);
Object val = map.get("Curry");
System.out.println("val=" + val);
System.out.println("k-v=" + map.size());
System.out.println(map.isEmpty());
System.out.println("map=" + map);
System.out.println("结果=" + map.containsKey("Curry"));
1.2 遍历Map
- 可以直接通过Map得到Key,然后通过Key得到Value来遍历
- 通过entryset来遍历
public static void main(String[] args) {
Map map = new HashMap();
map.put("Curry", "MVP");
map.put("Stephen", "MVP");
map.put("James", null);
map.put(null, "James");
Set keyset = map.keySet();
System.out.println("-----第一种方式-------");
for (Object key : keyset) {
System.out.println(key + "-" + map.get(key));
}
System.out.println("----第二种方式--------");
Iterator iterator = keyset.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key + "-" + map.get(key));
}
Collection values = map.values();
System.out.println("---取出所有的value 增强for----");
for (Object value : values) {
System.out.println(value);
}
System.out.println("---取出所有的value 迭代器----");
Iterator iterator2 = values.iterator();
while (iterator2.hasNext()) {
Object value = iterator2.next();
System.out.println(value);
}
Set entrySet = map.entrySet();
System.out.println("----使用EntrySet 的 for增强(第3种)----");
for (Object entry : entrySet) {
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "-" + m.getValue());
}
System.out.println("----使用EntrySet 的 迭代器(第4种)----");
Iterator iterator3 = entrySet.iterator();
while (iterator3.hasNext()) {
Object entry = iterator3.next();
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "-" + m.getValue());
}
}
2 HashMap
3 Hashtable
4 Properties