public static void main(String[] args) {
//创建一个HashMap
HashMap<String, Integer> prices = new HashMap<>();
// 往 HashMap 插入映射
prices.put("Shoes", 200);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
int returnedValue = prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue);
System.out.println("Price of Shirt: " + returnedValue);
// 输出更新后的 HashMap
System.out.println("Updated HashMap: " + prices);
}
网友评论