Map<String,Integer> m;
m = new TreeMap<String,Integer>();
当m.get()为空时,为了避免空指针异常,添加以下转换是否是好的做法。
System.out.println( ((Integer) 8).equals( m.get("null") ) ); // returns false
如果使用之前的空值检查,它开始看起来有点丑陋。
System.out.println( m.contains("null") != null && m.get("null").equals( 8 ) );
有没有更好的写法?谢谢。