一、集合框架(HashMap和Hashtable的区别)

一、HashMap和Hashtable

  都实现了Map接口,都是以key-value形式保存数据。

区别一:  HashMap可以存放null

  Hashtable不能存放null

区别二:  HashMap不是线程安全类

  Hashtable是线程安全类

package collection;
import java.util.HashMap;
import java.util.Hashtable;
public class TestCollection{
    //HashMap和Hashtable都实现了Map接口,key-value方式存数据
    HashMap<string,string> hashMap=new HashMap<string,string>();
    //HashMap可以用null作key,作value
    hashMap.put(null,"123");
    hashMap.put("123",null);
    Hashtable<string,string> hashtable=new Hashtable<string,string>();
    //Hashtable不能用null作key,不能用null作value
    hashtable.put(null,"123");//报错
    hashtable.put("123",null);//报错
}
View Code
原文地址:https://www.cnblogs.com/drq1/p/8482657.html