Java Map的迭代方式主要有以下几种:
以下是使用这些迭代方式的示例代码:
import java.util.*;
public class MapIterationExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("apple", 1);
        map.put("banana", 2);
        map.put("orange", 3);
        // 使用keySet()迭代Map
        System.out.println("Using keySet():");
        for (String key : map.keySet()) {
            System.out.println(key + " -> " + map.get(key));
        }
        // 使用values()迭代Map
        System.out.println("\nUsing values():");
        for (Integer value : map.values()) {
            System.out.println(value);
        }
        // 使用entrySet()迭代Map
        System.out.println("\nUsing entrySet():");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}
输出结果:
Using keySet():
apple -> 1
banana -> 2
orange -> 3
Using values():
1
2
3
Using entrySet():
apple -> 1
banana -> 2
orange -> 3
这些迭代方式都可以方便地遍历Map中的元素,具体选择哪种方式取决于你的需求。