資料來源:https://blog.csdn.net/q5706503/article/details/85122343
import java.util.*;
public class MapExample {
public static void main(String[] args) {
// 創建一個Map,使用String作為鍵,Integer作為值
Map<String, Integer> map = new HashMap<>();
// 添加一些鍵值對到Map中
map.put("apple", 5);
map.put("banana", 3);
map.put("cherry", 8);
// 使用entrySet()遍歷Map中的每一個鍵值對
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey(); // 獲取當前鍵值對的鍵
Integer value = entry.getValue(); // 獲取當前鍵值對的值
System.out.println("Key: " + key + ", Value: " + value);
}
}
}