HashMap和Hashtable的区别
1、* Hashtable是JDK1.0版本出现的,是线程安全的,效率低,HashMap是JDK1.2版本出现的,是线程不安全的,效率高;
* Hashtable不可以存储null键和null值,HashMap可以存储null键和null值;
2、示例代码如下:
package com.jh.www;
import java.util.HashMap;
import java.util.Hashtable;
public class Test7 {
public static void main(String[] args) {
HashMap<String,Integer> hm = new HashMap<>();
hm.put("张三", 18);
hm.put("李四", null);
hm.put(null, 20);
System.out.println(hm);
//报错:java.lang.NullPointerException 空指针异常
Hashtable<String,Integer> ht = new Hashtable<>();
ht.put("张三", 18);
ht.put("李四", null);
ht.put(null, 20);
System.out.println(ht);
}
}