Java中的LinkedHashSet源碼解讀
更新時(shí)間:2023年09月04日 09:48:36 作者:伊顰伊笑
這篇文章主要介紹了Java中的LinkedHashSet源碼解讀,LinkedHashSet?是?Java?中的一個(gè)集合類,它是?HashSet?的子類,并實(shí)現(xiàn)了?Set?接口,與?HashSet?不同的是,LinkedHashSet?保留了元素插入的順序,并且具有?HashSet?的快速查找特性,需要的朋友可以參考下
LinkedHashSet

基本介紹
- LinkedHashSet 是 HashSet 的子類
- LinkedHashSet 底層是一個(gè) LinkedHashMap,底層維護(hù)了一個(gè)數(shù)組+雙向鏈表
- LinkedHashSet 根據(jù)元素的 hashCode 值來(lái)決定元素的存儲(chǔ)位置,同時(shí)使用鏈表維護(hù)元素的次序(圖),這使得元素看起來(lái)是以插入順序保存的。
- LinkedHashSet 不允許添重復(fù)元素
說(shuō)明

- 在 LinkedHastSet 中維護(hù)了一個(gè) hash 表和雙向鏈表(LinkedHashSet 有head和tail)
- 每一個(gè)節(jié)點(diǎn)有 pre 和 next 屬性,這樣可以形成雙向鏈表
- 在添加一個(gè)元素時(shí),先求hash值,再求索引,確定該元素在hashtable 的位置,然后將添加的元素加入到雙向鏈表(如果已經(jīng)存在,不添加【原則和 hashset 一樣】)
tail.next = newElement//簡(jiǎn)單指定 newElement.pre = tail tail = newEelment;
- 這樣的話,我們遍歷 LinkedHashSet 也能確保插入順序和遍歷順序一致
LinkedHashSet 源碼解讀
package collection_;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 11:12 2021/9/22
*/
public class LinkedHashSetSource {
public static void main(String[] args) {
// 分析 LinkedHashSet 的底層機(jī)制
Set set = new LinkedHashSet();
set.add(new String("AAA"));
set.add(456);
set.add(456);
set.add(new Customer("Tom", 1));
set.add(123);
set.add("Gin");
System.out.println("set = " + set);
// 解讀:
// 1. LinkedHashSet 中元素的取出順序和加入順序一致
// 2. LinkedHashSet(HashSet的子類) 底層其實(shí)是 LinkedHashMap(HashMap的子類)
// 3. LinkedHashSet 底層結(jié)構(gòu)(table數(shù)組 + 雙向鏈表)
// 4. 第一次添加數(shù)據(jù),將 table數(shù)組 直接擴(kuò)容至 16 個(gè)空間大小
// 5. table數(shù)組的類型為 HashMap$Node[]
// 6. table數(shù)組中存放數(shù)據(jù)的結(jié)點(diǎn)類型是 LinkedHashMap$Entry
/*
LinkedHashMap 的靜態(tài)內(nèi)部類 Entry 繼承自 HashMap 的靜態(tài)內(nèi)部類 Node
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after; // before, after 用來(lái)形成雙向鏈表
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
*/
}
}
class Customer{
private String name;
private int no;
public Customer(String name, int no) {
this.name = name;
this.no = no;
}
}


到此這篇關(guān)于Java中的LinkedHashSet源碼解讀的文章就介紹到這了,更多相關(guān)LinkedHashSet源碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot詳解如何整合Redis緩存驗(yàn)證碼
本文主要介紹了SpringBoot集成Redis實(shí)現(xiàn)驗(yàn)證碼的緩存簡(jiǎn)單案例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決ObjectMapper.convertValue() 遇到的一些問(wèn)題
這篇文章主要介紹了解決ObjectMapper.convertValue() 遇到的一些問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot 整合 ShedLock 處理定時(shí)任務(wù)重復(fù)執(zhí)行的問(wèn)題小結(jié)
ShedLock是解決分布式系統(tǒng)中定時(shí)任務(wù)重復(fù)執(zhí)行問(wèn)題的Java庫(kù),通過(guò)在數(shù)據(jù)庫(kù)中加鎖,確保只有一個(gè)節(jié)點(diǎn)在指定時(shí)間執(zhí)行任務(wù),它與SpringScheduler、Quartz等框架結(jié)合使用,本文介紹Spring Boot 整合 ShedLock 處理定時(shí)任務(wù)重復(fù)執(zhí)行的問(wèn)題,感興趣的朋友一起看看吧2025-02-02
Spring Boot開(kāi)啟遠(yuǎn)程調(diào)試的方法
這篇文章主要介紹了Spring Boot開(kāi)啟遠(yuǎn)程調(diào)試的方法,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下2020-10-10
Java實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

