1.Map:双列集合,每个元素有两个数据,称为键值对,键值一一对应。 

键值对的键是不能重复的,而值是可以重复的。

集合框架(二)Map简述_键值对

2.Map特点(由键决定,值只是一个附属品)

Map<String, Integer> map1 = new HashMap<>();
  • 1.

(1)HashMap(由键决定):无序、不重复、无索引,(常用)

public class test {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>(); // 创建一个HashMap

        // 添加键值对
        map.put("手机", 100);
        map.put("手表", 220);
        map.put("手机", 2);  // 覆盖已有的键 "手机" 的值
        map.put("Java", 2);
        map.put(null, null); // 可以添加 null 键和 null 值

        // 输出 map
        System.out.println(map);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

(2)LinkedHashMap(由键决定):有序、不重复、无索引。

(3)TreeMap(由键决定):按大小默认升序、不重复、无索引。

3.常用方法

集合框架(二)Map简述_System_02

putAll:把其他Map集合倒进自己中来。重复的会覆盖。

4.Map的遍历方式

集合框架(二)Map简述_键值对_03

 (1)

public class test {
    public static void main(String[] args) {
        // 准备一个Map集合
        Map<String, Double> map = new HashMap<>();
        map.put("蜘蛛精", 162.5);
        map.put("蜘蛛精", 169.8); // 覆盖原有值
        map.put("紫霞", 165.8);
        map.put("至尊宝", 169.5);
        map.put("牛魔王", 183.6);
        
        // 打印整个map
        System.out.println(map);

        // 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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

(2)entrySet:把键值对封装起来。

public class test {
    public static void main(String[] args) {
        Map<String, Double> map = new HashMap<>();
        map.put("蜘蛛精", 169.8);
        map.put("紫霞", 165.8);
        map.put("至尊宝", 169.5);
        map.put("牛魔王", 183.6);

        System.out.println(map);

        // 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);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

(3)forEach

public class test {
    public static void main(String[] args) {
        Map<String, Double> map = new HashMap<>();
        map.put("蜘蛛精", 169.8);
        map.put("紫霞", 165.8);
        map.put("至尊宝", 169.5);
        map.put("牛魔王", 183.6);

        System.out.println(map);

        // 使用 Lambda 表达式遍历 Map
        map.forEach((k, v) -> {
            System.out.println(k + " ===> " + v);
        });
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

案例:想去景点人数统计

 

public class test {
    public static void main(String[] args) {
        List<String> data = new ArrayList<>();
        String[] selects = {"A", "B", "C", "D"};
        Random r = new Random();

        // 1. 模拟每个学生选择一个结果,存入集合中
        for (int i = 1; i <= 80; i++) {
            int index = r.nextInt(selects.length); // 生成0到3之间的随机整数
            data.add(selects[index]);
        }
        System.out.println(data);

        // 2. 开始统计每个结果的投票人数
        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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.