prices.merge

2021-04-14  本文已影响0人  felixfeijs
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);
    }
上一篇 下一篇

猜你喜欢

热点阅读