在Java中,可以使用put()方法向HashMap中添加元素。示例如下:
put()
HashMap<String, Integer> hashMap = new HashMap<>(); hashMap.put("key1", 100); hashMap.put("key2", 200); hashMap.put("key3", 300);
上面的代碼創建了一個HashMap對象,并使用put()方法向HashMap中添加了三個鍵值對。