HashMap和Hashtable及HashSet的区别

来源:互联网 发布:淘宝口红正品店铺 编辑:程序博客网 时间:2024/05/16 08:37

Hashtable继承Map接口,实现一个key-value映射的哈希表

HashMap和Hashtable类似,不同之处在于HashMap是非同步的,并且允许null,即null   value和null   key。

HashSet是一种不包含重复的元素的Collection,但不提供排序保证


Set hashSet = new HashSet();
   hashSet.add(new String("aaa"));
   hashSet.add(new String("bbb"));
   hashSet.add(new String("ccc"));
   hashSet.add(new String("aaa"));
   hashSet.add(new String("aaa"));


使用迭代器输出HashSet中的元素,例如:

Iterator it = hashSet.iterator();
   while(it.hasNext()){
    System.out.println((String)it.next());
   }


原创粉丝点击