Java 中 Auto-boxing/unboxing 机制,在合适的时机自动打包,解包.
1. 自动将基础类型转换为对象;
2. 自动将对象转换为基础类型;
Demo_1:
import java.util.*; class Test { public static void main(String[] args ){ HashMap<String, Integer> mp1 = new HashMap<String, Integer>(); HashMap<String, Integer> mp2 = new HashMap<String, Integer>(); mp1.put("one", 1); mp1.put("two", 2); mp1.put("three",3); mp2.put("A", 1); mp2.put("B", 2); System.out.println(mp1.size()); // 输出:3 System.out.println(mp1.containsKey("ONE")); // 输出:false System.out.println(mp2.containsValue(2)); // 输出:true if(mp1.containsKey("two")){ int i = mp1.get("two"); System.out.println(i); // 输出:2 } System.out.println(mp1); // 输出:{one=1, two=2, three=3} mp1.putAll(mp2); System.out.println(mp1); // 输出:{A=1, B=2, one=1, two=2, three=3} } }