Hashtable、HashMap和HashSet

1、Hashtable

Hashtable是一种基本的数据结构,用于保存key-value pair。

(1)它不允许key和value为null的情况,如果使用一个null value,就会报NullPointerException异常;

(2)它是同步的(线程安全),即在某个时间点只有一个线程访问内部资源。

Hashtable<Integer,String>; cityTable = new Hashtable<Integer,String>();
cityTable.put(1, "Lahore");
cityTable.put(2, "Karachi");
cityTable.put(3, null); /* NullPointerEcxeption at runtime*/
 
System.out.println(cityTable.get(1));
System.out.println(cityTable.get(2));
System.out.println(cityTable.get(3));

2、HashMap

HashMap也是用于保存key-value pair的。

(1)它允许key和value为null;

(2)它是异步的(非线程安全),对于单线程而言,可以大大提高性能。

HashMap<Integer,String> productMap = new HashMap<Integer,String>();
productMap.put(1, "Keys");
productMap.put(2, null);

3、HashSet

HashSet不允许重复的value。它只提供add方法而没有put方法。

如果你希望有一个唯一的非重复的list,那么HashSet是一个不错的选择。

HashSet<String> stateSet = new HashSet<String>();
stateSet.add ("CA");
stateSet.add ("WI");
stateSet.add ("NY");
 
if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/
     System.out.println("Already found");
else
    stateSet.add("PB");



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值