Loading

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

 

posted @ 2017-03-29 10:39  stono  阅读(5096)  评论(0编辑  收藏  举报