HashMap和Hashtable中的方法大多都是map中的,可以参考map的
HashMap
Hashtable 底层都是哈西表
同:
1.底层都是哈西表
不同:
1. Hashtable 第一次创建对象的时候就会给底层数组开辟空间 Entry[] 11
HashMap 创建对象时 没有给底层数组进行空间开辟
当添加数据时才进行空间开辟
2.Hashtable key value 都不能有null值
HashMap key value 都可以是null
3. Hashtable 线程安全的 效率低
HashMap 线程不安全的 效率高
4.HashMap 是jdk1.2
Hashtable 是jdk1.0
//初始化 加载因子0.75
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
public Hashtable() {
this(11, 0.75f);
}
public class HashtableTest {
@Test
public void test02(){
Hashtable hashtable = new Hashtable();
hashtable.put("a", null);
}
@Test
public void test01(){
HashMap map = new HashMap();
map.put(null, "张三");
map.put("a", null);
map.put(null, null);
System.out.println(map);
System.out.println(map.size());
}
}