点击运行
import java.util.HashMap; public class Main { public static void main(String[] args) { // 创建一个 HashMap HashMap
countries = new HashMap<>(); // 往HashMap插入映射项 countries.put("Washington", "America"); countries.put("Canberra", "Australia"); countries.put("Madrid", "Spain"); System.out.println("HashMap: " + countries); //合并 key为 Washington的映射 String returnedValue = countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue); System.out.println("Washington: " + returnedValue); //输出更新后的HashMap System.out.println("Updated HashMap: " + countries); } }
运行结果 :
正在执行...