- HashMap(由键决定特点):无序、不重复、无索引;
- LinkedHashMap:有序、不重复、无索引;
- TreeMap:按照大小默认升序排序、不重复、无索引;
public static void main(String[] args) {
// Map<String, Integer> map = new HashMap<>();
Map<String, Integer> map = new LinkedHashMap<>();
map.put("篮球鞋",10);
map.put("篮球鞋",20);//后面重复的数据会覆盖前面的数据
map.put("手机",100);
map.put("外套",30);
System.out.println(map);
Map<Integer, String> map1 = new TreeMap<>();
map1.put(23,"java");
map1.put(23,"java1");
map1.put(12,"java2");
map1.put(15,"java3");
System.out.println(map1);
}
常用方法
//获取大小
System.out.println(map.size());
//清空集合
// map.clear();
System.out.println(map);
//判断是否空
System.out.println(map.isEmpty());
//根据键获取对应值
Integer v1 = map.get("手机");
System.out.println(v1);
//根据键删除元素
System.out.println(map.remove("手机"));
System.out.println(map);
//判断是否包含某个键
System.out.println(map.containsKey("手机"));
System.out.println(map.containsKey("java"));
//判断是否包含某个值
System.out.println(map.containsValue(2));
System.out.println(map.containsValue(10));
//获取Map集合所有的键
Set<String> keys = map.keySet();
System.out.println(keys);
//获取所有的值
Collection<Integer> values = map.values();
System.out.println(values);
//把其他Map集合的数据倒入到自己的集合
Map<String, Integer> map1 = new HashMap<>();
map1.put("abc",10);
map1.put("abc1",20);
Map<String, Integer> map2 = new HashMap<>();
map2.put("zxc",10);
map2.put("zxc1",200);
map1.putAll(map2);
System.out.println(map1);
System.out.println(map2);
Map遍历方式
Map<String, Double> map = new HashMap<>();
map.put("熊大",177.6);
map.put("凡哥",188.1);
map.put("光头强",166.4);
System.out.println(map);
// {熊大=177.6, 光头强=166.4, 凡哥=188.1}
//1.获取Map集合所有的键
Set<String> keys = map.keySet();
// System.out.println(keys);//[熊大, 光头强, 凡哥]
//2.遍历所有的键
for (String key : keys) {
//3.根据键找对应的值
Double value = map.get(key);
System.out.println(key + "===>" + value);
}
//调用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);
}
map.forEach((k ,v) -> {
System.out.println(k + "===>" + v);
});
案例:统计投票人数
- 需求分析:某个班级80人,组织秋游活动,有A、B、C、D四个景点,每个人只能选择一个,统计出哪个景点想去的人最多
public static void main(String[] args) {
//1.把80个学生选择的景点数据拿到程序中
List<String> data = new ArrayList<>();
String select[] = {"A","B","C","D"};
Random r = new Random();
for (int i = 1; i <= 80; i++) {
//每次模拟一个学生选择一个景点,存入到集合中
int index = r.nextInt(4);
data.add(select[index]);
}
System.out.println(data);
//2.开始统计每个景点的投票人数
//准备一个map集合用于统计最终结果
Map<String, Integer> result = new HashMap<>();
//3.开始遍历80个景点数据
for (String s : data) {
//问问Map集合是否存在该景点
if(result.containsKey(s)){
//说明这个景点之前统计过,其值+1
result.put(s,result.get(s) + 1);
}else{
//说明这个景点是第一次存入统计,其值=1
result.put(s,1);
}
}
System.out.println(result);
}
底层原理
- HashMap:HashSet一样
- LinkedHashMap:和LinkedHashSet一样
- TreeMap:和TreeSet一样
Map集合案例-省和市(集合的嵌套ArrayList和HashMap共同实现)
- 需求:要求在程序中记住如下省份和其对应的城市信息,记录成功后,要求可以查询出湖北省的城市信息
public static void main(String[] args) {
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);
List<String> hubeiCities = map.get("湖北省");
for (String hubeiCity : hubeiCities) {
System.out.println(hubeiCity);
}
map.forEach((p,c ) -> {
System.out.println(p + "===>" + c);
});
}