Java-Collections Framework学习与总结-HashSet和LinkedHashSet

本篇总结一下两个常用的集合类HashSet和LinkedHashSet。
        它们都实现了相同接口java.util.Set。Set表示一种元素无序且不可重复的集合;之前总结过的java.util.List表示一种元素可重复且有序的集合。它们都扩展自java.util.Collection。
Java代码   收藏代码
  1. public interface Set<E> extends Collection<E> {  
  2.     int size();  
  3.     boolean isEmpty();  
  4.     boolean contains(Object o);  
  5.     Iterator<E> iterator();  
  6.     Object[] toArray();  
  7.     <T> T[] toArray(T[] a);  
  8.     boolean add(E e);  
  9.     boolean remove(Object o);  
  10.     boolean containsAll(Collection<?> c);  
  11.     boolean addAll(Collection<? extends E> c);  
  12.     boolean retainAll(Collection<?> c);  
  13.     boolean removeAll(Collection<?> c);  
  14.     void clear();  
  15.     boolean equals(Object o);  
  16.     int hashCode();  

        可以看到Set的接口描述,和List比,只少了一些与下标有关的特性。

        先来看一下HashSet的实现方式。
Java代码   收藏代码
  1. public class HashSet<E>  
  2.     extends AbstractSet<E>  
  3.     implements Set<E>, Cloneable, java.io.Serializable  
  4. {  

        HashSet是 可克隆、可序列化的;实现了java.util.Set;继承了java.util.AbstractSet,AbstractSet包含一些骨架方法,很容易看懂。继续往下看。
Java代码   收藏代码
  1. private transient HashMap<E,Object> map;  
  2.   
  3. // Dummy value to associate with an Object in the backing Map  
  4. private static final Object PRESENT = new Object();  

        看到这里就基本上明白是怎么实现的了吧!原来HashSet内部是一个HashMap,难怪HashSet用起来很像一个HashMap,只是没有value。所以PRESENT这个对象就用来填充value值了。有了前面 HashMap的分析,后面的基本上不用看了。
        不过有一个小地方要注意一下。
Java代码   收藏代码
  1.    /** 
  2.     * Constructs a new, empty linked hash set.  (This package private 
  3.     * constructor is only used by LinkedHashSet.) The backing 
  4.     * HashMap instance is a LinkedHashMap with the specified initial 
  5.     * capacity and the specified load factor. 
  6.     * 
  7.     * @param      initialCapacity   the initial capacity of the hash map 
  8.     * @param      loadFactor        the load factor of the hash map 
  9.     * @param      dummy             ignored (distinguishes this 
  10.     *             constructor from other int, float constructor.) 
  11.     * @throws     IllegalArgumentException if the initial capacity is less 
  12.     *             than zero, or if the load factor is nonpositive 
  13.     */  
  14.    HashSet(int initialCapacity, float loadFactor, boolean dummy) {  
  15. map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);  
  16.    }  

        这个构造方法和其他的不一样。里面的Map实现是LinkedHashMap。从注释上可以看到,这个方法只是由LinkedHashSet使用,方法的第三个参数也只是用来与其他构造方法进行区分,代码中并没有实际用到。

        接下来看一下java.util.LinkedHashSet的实现吧。
Java代码   收藏代码
  1. public class LinkedHashSet<E>  
  2.     extends HashSet<E>  
  3.     implements Set<E>, Cloneable, java.io.Serializable {  
  4.   
  5.     private static final long serialVersionUID = -2851667679971038690L;  
  6.   
  7.     /** 
  8.      * Constructs a new, empty linked hash set with the specified initial 
  9.      * capacity and load factor. 
  10.      * 
  11.      * @param      initialCapacity the initial capacity of the linked hash set 
  12.      * @param      loadFactor      the load factor of the linked hash set 
  13.      * @throws     IllegalArgumentException  if the initial capacity is less 
  14.      *               than zero, or if the load factor is nonpositive 
  15.      */  
  16.     public LinkedHashSet(int initialCapacity, float loadFactor) {  
  17.         super(initialCapacity, loadFactor, true);  
  18.     }  
  19.   
  20.     /** 
  21.      * Constructs a new, empty linked hash set with the specified initial 
  22.      * capacity and the default load factor (0.75). 
  23.      * 
  24.      * @param   initialCapacity   the initial capacity of the LinkedHashSet 
  25.      * @throws  IllegalArgumentException if the initial capacity is less 
  26.      *              than zero 
  27.      */  
  28.     public LinkedHashSet(int initialCapacity) {  
  29.         super(initialCapacity, .75f, true);  
  30.     }  
  31.   
  32.     /** 
  33.      * Constructs a new, empty linked hash set with the default initial 
  34.      * capacity (16) and load factor (0.75). 
  35.      */  
  36.     public LinkedHashSet() {  
  37.         super(16, .75f, true);  
  38.     }  
  39.   
  40.     /** 
  41.      * Constructs a new linked hash set with the same elements as the 
  42.      * specified collection.  The linked hash set is created with an initial 
  43.      * capacity sufficient to hold the elements in the specified collection 
  44.      * and the default load factor (0.75). 
  45.      * 
  46.      * @param c  the collection whose elements are to be placed into 
  47.      *           this set 
  48.      * @throws NullPointerException if the specified collection is null 
  49.      */  
  50.     public LinkedHashSet(Collection<? extends E> c) {  
  51.         super(Math.max(2*c.size(), 11), .75f, true);  
  52.         addAll(c);  
  53.     }  
  54. }  

        以上便是LinkedHashSet的全部代码了,基本上没什么可说的了,参考下 LinkedHashMap的总结
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值