在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