一、 概述
二、常用方法
三、遍历方式
//1.获取Map集合的全部键
Set<String> keys = map.keySet();
System.out.println(keys);
//2.遍历全部键,根据键获取全部的值
for (String key : keys) {
double value = map.get(key);
System.out.println(key + "--->" + value);
}
//1.调用Map集合提供的entrySet方法,把Map集合转换成键值对类型的Set集合
Set<Map.Entry<String, Double>> entries = map.entrySet();
for (Map.Entry<String, Double> entry : entries) {
String key = entry.getKey();
Double value = entry.getValue();
System.out.println(key + "--->" + value);
}
//3.Lambda
map.forEach((k, v) -> {
System.out.println(k + "--->" + v);
});
四、HashMap
五、LinkedHashMap
六、TreeMap
七、集合的嵌套
指的是集合中的元素又是一个集合
Map<String, List<String>> map = new HashMap<>();
List<String> cities1 = new ArrayList<>();
Collections.addAll(cities1,"南京市","扬州市");
map.put("江苏省",cities1);
List<String> cities2 = new ArrayList<>();
Collections.addAll(cities2,"武汉市","孝感市");
map.put("湖北省",cities2);
List<String> cities3 = new ArrayList<>();
Collections.addAll(cities3,"石家庄市","唐山市");
map.put("河北省",cities3);
System.out.println(map);