TreeMap、HashMap、HashTable的键是否能为""和Null的问题
2012-03-13 10:56
375 查看
TreeMap键不能为null,HashMap键可以为null。
实例代码:
Map<Object,String> treeMap_1 = new TreeMap<Object,String>();
treeMap_1.put(null, "treeMap_1成功了!");
Map<String,Object> hashMap_1 = new HashMap<String,Object>();
hashMap_1.put(null, new String("hashMap_1,ok......"));
System.out.println(treeMap_1.get(null));//运行时产生java.lang.NullPointerException异常
System.out.println(hashMap_1.get(null));//打印hashMap_1,ok......
或者:
Map<Object,String> treeMap_2 = new TreeMap<Object,String>();
treeMap_2.put(null, "treeMap_2成功了!");
Map<Object,String> hashMap_2 = new HashMap<Object,String>();
hashMap_2.put(null, "hashMap_2,ok......");
System.out.println(treeMap_2.get(null));//运行时产生java.lang.NullPointerException异常
System.out.println(hashMap_2.get(null));//打印hashMap_2,ok......
TreeMap键可以为"",HashMap键可以为""。
实例代码:
Map<Object,String> treeMap_1 = new TreeMap<Object,String>();
treeMap_1.put(new String(""), "treeMap_1成功了!");
Map<String,Object> hashMap_1 = new HashMap<String,Object>();
hashMap_1.put("", new String("hashMap_1,ok......"));
System.out.println(treeMap_1.get(""));//打印treeMap_1,ok......
System.out.println(hashMap_1.get(""));//打印hashMap_1,ok......
或者:
Map<Object,String> treeMap_2 = new TreeMap<Object,String>();
treeMap_2.put(new String(""), "treeMap_2成功了!");
Map<Object,String> hashMap_2 = new HashMap<Object,String>();
hashMap_2.put(new String(""), "hashMap_2,ok......");
System.out.println(treeMap_2.get(""));//打印treeMap_2,ok......
System.out.println(hashMap_2.get(""));//打印hashMap_2,ok......
HashTable与TreeMap相同,键不能为null,可以为""。
Map<Object,Object> table_1 = new Hashtable<Object,Object>();
table_1.put(new String(""), new String("table_1"));
System.out.println(table_1.get(""));//打印table_1
Map<String,Object> table_2 = new Hashtable<String,Object>();
table_2.put("", new String("table_2"));
System.out.println(table_2.get(""));//打印table_2
-------------------------------------------------------------
Map<Object,Object> table_1 = new Hashtable<Object,Object>();
table_1.put(null, new String("table_1"));
System.out.println(table_1.get(null));//运行时产生java.lang.NullPointerException异常
Map<String,Object> table_2 = new Hashtable<String,Object>();
table_2.put(null, new String("table_2"));
System.out.println(table_2.get(null));//运行时产生java.lang.NullPointerException异常
实例代码:
Map<Object,String> treeMap_1 = new TreeMap<Object,String>();
treeMap_1.put(null, "treeMap_1成功了!");
Map<String,Object> hashMap_1 = new HashMap<String,Object>();
hashMap_1.put(null, new String("hashMap_1,ok......"));
System.out.println(treeMap_1.get(null));//运行时产生java.lang.NullPointerException异常
System.out.println(hashMap_1.get(null));//打印hashMap_1,ok......
或者:
Map<Object,String> treeMap_2 = new TreeMap<Object,String>();
treeMap_2.put(null, "treeMap_2成功了!");
Map<Object,String> hashMap_2 = new HashMap<Object,String>();
hashMap_2.put(null, "hashMap_2,ok......");
System.out.println(treeMap_2.get(null));//运行时产生java.lang.NullPointerException异常
System.out.println(hashMap_2.get(null));//打印hashMap_2,ok......
TreeMap键可以为"",HashMap键可以为""。
实例代码:
Map<Object,String> treeMap_1 = new TreeMap<Object,String>();
treeMap_1.put(new String(""), "treeMap_1成功了!");
Map<String,Object> hashMap_1 = new HashMap<String,Object>();
hashMap_1.put("", new String("hashMap_1,ok......"));
System.out.println(treeMap_1.get(""));//打印treeMap_1,ok......
System.out.println(hashMap_1.get(""));//打印hashMap_1,ok......
或者:
Map<Object,String> treeMap_2 = new TreeMap<Object,String>();
treeMap_2.put(new String(""), "treeMap_2成功了!");
Map<Object,String> hashMap_2 = new HashMap<Object,String>();
hashMap_2.put(new String(""), "hashMap_2,ok......");
System.out.println(treeMap_2.get(""));//打印treeMap_2,ok......
System.out.println(hashMap_2.get(""));//打印hashMap_2,ok......
HashTable与TreeMap相同,键不能为null,可以为""。
Map<Object,Object> table_1 = new Hashtable<Object,Object>();
table_1.put(new String(""), new String("table_1"));
System.out.println(table_1.get(""));//打印table_1
Map<String,Object> table_2 = new Hashtable<String,Object>();
table_2.put("", new String("table_2"));
System.out.println(table_2.get(""));//打印table_2
-------------------------------------------------------------
Map<Object,Object> table_1 = new Hashtable<Object,Object>();
table_1.put(null, new String("table_1"));
System.out.println(table_1.get(null));//运行时产生java.lang.NullPointerException异常
Map<String,Object> table_2 = new Hashtable<String,Object>();
table_2.put(null, new String("table_2"));
System.out.println(table_2.get(null));//运行时产生java.lang.NullPointerException异常
相关文章推荐
- 关于TreeMap、HashMap、HashTable的键是否能为""和Null的问题
- 关于TreeMap、HashMap、HashTable的键是否能为\"\"和Null的问题
- HashMap、HashTable的key和value是否可为null 或者 ""
- HashMap HashTable ConcurrentHashMap key和value是否可以null的问题 源码分析
- HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序
- HashMap HashTable ConcurrentHashMap key和value是否可以null的问题 源码分析
- Oracle关于varchar2型中"(空字符串)是否等于NULL问题
- js里面null,0,"",false,undefined是否相等问题
- HashMap、HashTable的key和value是否可为null
- FORM的ENCTYPE="multipart/form-data" 时request.getParameter()值为null问题的解决
- String中null变为"null"字符串的问题
- Python美女[从新手到高手]--阅读"见个面问题 HashMap 储存方法"联想
- 【Stackoverflow好问题】祛烦“!=null"()判处空语句
- HashMap、HashTable的key和value是否可为null
- "设备未准备好。如果目标是ActiveSync 设备,请验证ActiveSync 连接是否在运行"问题的解决
- open("/dev/null",o_rdwr)的问题
- HashMap、HashTable的key和value是否可为null
- 判断字符串是否为空 string != null && !string.equals("")
- "设备未准备好。如果目标是ActiveSync 设备,请验证ActiveSync 连接是否在运行"问题的解决
- ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap