JDK源碼分析之Set類詳解

JDK源碼分析Set類,因爲Set類是經常要用到的,那我們知道JDK源碼中Set類在其中不可以有相同的元素,那麼判斷這個元素是否相同是如何實現的呢,我們看下下面這張圖:

JDK源碼分析之Set類圖  

對JDK源碼分析之Set類在這張類圖上,首先我們看見一個經典模式的應用,那就是適配器模式,我們把map接口的對象,包裝成爲了Set的接口;在代碼中,我們來分析一下;

首先,我們看一下HashSet

  1. private transient HashMap map;  
  2.  
  3.    // Dummy value to associate with an Object in the backing Map  
  4.    private static final Object PRESENT = new Object(); 

可見,他適配了HashMap,那麼他的功能是如何委託給HashMap結構的呢?

  1. public boolean add(E e) {  
  2.    return map.put(e, PRESENT)==null;  
  3.    } 

在HashMap中,我們大多數時候是用value,但是在set的時候,卻很好的利用了已有類HashMap,他利用了HashMap的key的唯一性來保證存儲在Set中的元素的唯一性;

private static final Object PRESENT = new Object();

是這個HashMap所有key的value,他只是一個形式,而我們真正的數據是存在在key中的資源;

我們最後拿到的迭代器也是:

  1. public Iterator iterator() {  
  2.   return map.keySet().iterator();  
  3.   } 

Map的keySet的迭代器;

同理,我們看看LinkedhashMap;

  1. public LinkedHashSet(int initialCapacity, float loadFactor) {  
  2.        super(initialCapacity, loadFactor, true);  
  3.    }  
  4.  
  5.    /**  
  6.     * Constructs a new, empty linked hash set with the specified initial  
  7.     * capacity and the default load factor (0.75).  
  8.     *  
  9.     * @param   initialCapacity   the initial capacity of the LinkedHashSet  
  10.     * @throws  IllegalArgumentException if the initial capacity is less  
  11.     *              than zero  
  12.     */ 
  13.    public LinkedHashSet(int initialCapacity) {  
  14.        super(initialCapacity, .75f, true);  
  15.    }  
  16.  
  17.    /**  
  18.     * Constructs a new, empty linked hash set with the default initial  
  19.     * capacity (16) and load factor (0.75).  
  20.     */ 
  21.    public LinkedHashSet() {  
  22.        super(16, .75f, true);  
  23.    } 

調用了父類的構造函數;構造函數如下:

  1. HashSet(int initialCapacity, float loadFactor, boolean dummy) {  
  2.  map = new LinkedHashMap(initialCapacity, loadFactor);  
  3.  } 

生出了LinkedHashMap;

同理,我們一樣可見到TreeMap的實現:

  1. private transient NavigableMap m;  
  2.  
  3. // Dummy value to associate with an Object in the backing Map  
  4. private static final Object PRESENT = new Object(); 

更多的,我們也可以理解他是一種橋接模式的一種變形,不過我想從意義上,我更願意相信其是適配器的應用;

對JDK源碼分析之Set類到這裏,希望對你有幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章