java中的常用数据结构及类型转换

    public static void main(String[] args) {
        Long a = 44l;
        Long b=null;

        HashMap map = new HashMap<>();
        map.put("lirui",a);
        map.put("lizhi",b);

        Object lirui = map.get("lirui");
        //String haha = (String)lirui;//报错 java.lang.Long cannot be cast to java.lang.String
        if (lirui!=null){
            Long c = Long.valueOf(String.valueOf(lirui));
            System.out.println(c);
        }

        Object lizhi = map.get("lizhi");
        Long d = null;
        if (lizhi!=null){
            d = Long.valueOf(String.valueOf(lirui));
            System.out.println(d);
        }

    }

你可能感兴趣的:(java,Object,Long和String转换,java)