• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Foreordination
酒后高歌磨剑,梦中快意恩仇,名利脚下踩,情义两肩挑
博客园    首页    新随笔    联系   管理    订阅  订阅
一、集合框架(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

 

posted on 2018-02-28 11:24  Foreordination  阅读(160)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3