实例 Java 创建一个存储字符串的键和整数的值的 HashMap

x
 
// Import the HashMap class
import java.util.HashMap;
public class Main {
  public static void main(String[] args) {
    // Create a HashMap object called people
    HashMap<String, Integer> people = new HashMap<String, Integer>();
    // Add keys and values (Name, Age)
    people.put("John", 32);
    people.put("Steve", 30);
    people.put("Angie", 33);
    for (String i : people.keySet()) {
      System.out.println("Name: " + i + " Age: " + people.get(i));
    }
  }
}
                    

输出结果

Name: Angie Age: 33
Name: Steve Age: 30
Name: John Age: 32