JAVA - 无法添加到HashMap

Joh*_*ack 2 java add hashmap map

我是JAVA的新手,我写了这段代码:

Map<String,Integer> map = new HashMap<String,Integer>();
map.add("Key",13);
Run Code Online (Sandbox Code Playgroud)

为什么它会在eclipse上给出错误:

The method add(String, int) is undefined for the type Map<String,Integer>
Run Code Online (Sandbox Code Playgroud)

Kon*_*Kon 7

没有add方法.你想要的方法是put.

map.put("Key", 13);
Run Code Online (Sandbox Code Playgroud)

在这里查看Javadocs:http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html#put(K ,V)