题目:
List<String> aList = new ArrayList<String>();
aList.add(null);
Set aset = new HashSet();
aset.add(null);
Map<String,String> aMap = new HashMap<String,String>();
aMap.put(null,null);
Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
attributeTypes.put("String",null);
attributeTypes.put(null,null);
答案:
HashTable会对null抛出NullException。从HashTable的源码得知。当key为空时,会对外抛出NullException.
而List把null当做正常的Object来处理。
而Map针对null有单独的处理代码。Set内部的实现时采纳Map,自然就不会抛出NullException
List<String> aList = new ArrayList<String>();
aList.add(null);
Set aset = new HashSet();
aset.add(null);
Map<String,String> aMap = new HashMap<String,String>();
aMap.put(null,null);
Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
attributeTypes.put("String",null);
attributeTypes.put(null,null);
答案:
HashTable会对null抛出NullException。从HashTable的源码得知。当key为空时,会对外抛出NullException.
而List把null当做正常的Object来处理。
而Map针对null有单独的处理代码。Set内部的实现时采纳Map,自然就不会抛出NullException