点击运行
import java.util.HashMap; public class Main { public static void main(String[] args) { //创建一个HashMap HashMap
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); } }
运行结果 :
正在执行...