@[TOC]一个程序搞懂hashMap和hashTable键值对的区别
hashMap 的[key]和[value]均可以为 null:null
hashTable 的[key]不能为 null
hashTable 的[value]不能为 null
hashTable 的[value]不能为 null
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map <String, String> map = new HashMap <String, String>();//HashMap 对象
Map<String, String> tableMap = new Hashtable <String, String>();//HashTable 对象
map.put(null, null);
System.out.println("hashMap 的[key]和[value]均可以为 null:" + map.get(null));
try {
tableMap.put(null, "3");
System.out.println(tableMap.get(null));
} catch (Exception e) {
System.out.println("【ERROR】:hashTable 的[key]不能为 null");
}
try {
tableMap.put("3", null);
System.out.println(tableMap.get("3"));
} catch (Exception e) {
System.out.println("【ERROR】:hashTable 的[value]不能为 null");
}
try {
tableMap.put(null, null);
System.out.println(tableMap.get(null));
} catch (Exception e) {
System.out.println("【ERROR】:hashTable 的[value]不能为 null");
}
}
}