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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
相关资源
相似解决方案