W3School TIY Editor

  • W3School 在线教程
  • 改变方向
  • 暗黑模式
​x
 
// 导入 HashMap 类
import java.util.HashMap;
​
public class Main {
  public static void main(String[] args) {
​
    // 创建一个名为 people 的 HashMap 对象
    HashMap<String, Integer> people = new HashMap<String, Integer>();
​
    // 添加键值对 (姓名, 年龄)
    people.put("Bill", 32);
    people.put("Elon", 30);
    people.put("Steven", 33);
​
    for (String i : people.keySet()) {
      System.out.println("姓名: " + i + " 年龄: " + people.get(i));
    }
  }
}