新聞中心
這篇文章主要介紹“JDK的Set源碼分析”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“JDK的Set源碼分析”文章能幫助大家解決問題。
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、市中網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為市中等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
首先,我們看一下HashSet
private transient HashMap
map; // Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
可見,他適配了HashMap,那么他的功能是如何委托給HashMap結(jié)構(gòu)的呢?
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
在hashMap中,我們大多數(shù)時候是用value,但是在set的時候,卻很好的利用了已有類HashMap,他利用了HashMap的key的唯一性來保證存儲在Set中的元素的唯一性;
private static final Object PRESENT = new Object();
是這個HashMap所有key的value,他只是一個形式,而我們真正的數(shù)據(jù)是存在在key中的資源;
我們最后拿到的迭代器也是:
public Iterator
iterator() { return map.keySet().iterator();
}
map的keySet的迭代器;
同理,我們看看LinkedhashMap;
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
/**
* Constructs a new, empty linked hash set with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity of the LinkedHashSet
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}
/**
* Constructs a new, empty linked hash set with the default initial
* capacity (16) and load factor (0.75).
*/
public LinkedHashSet() {
super(16, .75f, true);
}
調(diào)用了父類的構(gòu)造函數(shù);構(gòu)造函數(shù)如下:
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap
(initialCapacity, loadFactor); }
生出了LinkedHashMap;
同理,我們一樣可見到treeMap的實現(xiàn):
private transient NavigableMap
m; // Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
關(guān)于“JDK的Set源碼分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
網(wǎng)站題目:JDK的Set源碼分析
網(wǎng)址分享:http://fisionsoft.com.cn/article/jiedio.html