在Map對象中獲取屬性,注意判斷為空


在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);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM