在Map對象中獲取屬性,注意判斷為空
public static void main(String[] args) { Map map = new HashMap(); Integer i = (Integer) map.get("aaa"); System.out.println(i); // 這樣返回的是null }
注意map.get不具備自動轉換的功能;
public static void main(String[] args) { Map map = new HashMap(); map.put("aaa", "1"); Integer i = (Integer) map.get("aaa"); // 這樣就會報異常了,String cannot cast to Integer System.out.println(i); }