源码

package com.codesensi.demo01;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {

        // 组织Map集合
        Map<String, String> map = new HashMap<>();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");
        map.put("4", "value4");

        //第一种
        System.out.println("--------------------华丽的分割线--------------------------");
        System.out.println("通过Map.keySet遍历key和value:");
        for (String key : map.keySet()) {
            System.out.println("Key: " + key + ", Value: " + map.get(key));
        }

        //第二种
        System.out.println("--------------------华丽的分割线--------------------------");
        System.out.println("通过Map.entrySet使用iterator遍历key和value: ");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }

        //第三种
        System.out.println("--------------------华丽的分割线--------------------------");
        System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
        for (String v : map.values()) {
            System.out.println("The value is " + v);
        }
    }
}

结果

--------------------华丽的分割线--------------------------
通过Map.keySet遍历key和value:
Key: 1, Value: value1
Key: 2, Value: value2
Key: 3, Value: value3
Key: 4, Value: value4
--------------------华丽的分割线--------------------------
通过Map.entrySet使用iterator遍历key和value: 
Key: 1, Value: value1
Key: 2, Value: value2
Key: 3, Value: value3
Key: 4, Value: value4
--------------------华丽的分割线--------------------------
通过Map.values()遍历所有的value,但不能遍历key
The value is value1
The value is value2
The value is value3
The value is value4

Q.E.D.