Golang HashMap實(shí)現(xiàn)原理解析
- HashMap是一種基于哈希表實(shí)現(xiàn)的鍵值對(duì)存儲(chǔ)結(jié)構(gòu),它通過(guò)哈希函數(shù)將鍵映射到數(shù)組的索引位置,支持高效的插入、查找和刪除操作。其核心原理如下:
- 哈希函數(shù):將鍵轉(zhuǎn)換為數(shù)組索引。理想情況下,不同鍵應(yīng)映射到不同索引,但沖突難以避免。
- 數(shù)組+鏈表:使用數(shù)組作為桶(Bucket),每個(gè)桶是一個(gè)鏈表,解決哈希沖突(鏈地址法)。
- 動(dòng)態(tài)擴(kuò)容:當(dāng)元素?cái)?shù)量超過(guò)容量與負(fù)載因子的乘積時(shí),擴(kuò)容并重新分配元素,保持操作高效性。
package main import "fmt" // Entry 鍵值對(duì)鏈表節(jié)點(diǎn) type Entry struct { Key string Value interface{} Next *Entry } // HashMap 哈希表結(jié)構(gòu) type HashMap struct { buckets []*Entry // 桶數(shù)組 capacity int // 初始容量 size int // 元素?cái)?shù)量 loadFactor float64 // 負(fù)載因子 } // NewHashMap 初始化哈希表 func NewHashMap(capacity int) *HashMap { return &HashMap{ buckets: make([]*Entry, capacity), capacity: capacity, loadFactor: 0.75, } } // hash 哈希函數(shù)(FNV-1a算法) func (h *HashMap) hash(key string) int { const ( offset = 2166136261 prime = 16777619 ) hash := offset for _, c := range key { hash ^= int(c) hash *= prime } return hash } // getIndex 計(jì)算鍵對(duì)應(yīng)的桶索引 func (h *HashMap) getIndex(key string) int { return h.hash(key) % h.capacity } // Put 插入鍵值對(duì) func (h *HashMap) Put(key string, value interface{}) { if float64(h.size)/float64(h.capacity) >= h.loadFactor { h.resize() } index := h.getIndex(key) entry := h.buckets[index] // 遍歷鏈表查找鍵是否存在 for entry != nil { if entry.Key == key { entry.Value = value // 存在則更新 return } entry = entry.Next } // 不存在則插入鏈表頭部 h.buckets[index] = &Entry{ Key: key, Value: value, Next: h.buckets[index], } h.size++ } // Get 獲取值 func (h *HashMap) Get(key string) (interface{}, bool) { index := h.getIndex(key) entry := h.buckets[index] for entry != nil { if entry.Key == key { return entry.Value, true } entry = entry.Next } return nil, false } // Delete 刪除鍵 func (h *HashMap) Delete(key string) bool { index := h.getIndex(key) entry := h.buckets[index] var prev *Entry for entry != nil { if entry.Key == key { if prev == nil { h.buckets[index] = entry.Next // 刪除頭節(jié)點(diǎn) } else { prev.Next = entry.Next // 中間或尾部節(jié)點(diǎn) } h.size-- return true } prev = entry entry = entry.Next } return false } // resize 擴(kuò)容哈希表 func (h *HashMap) resize() { newCapacity := h.capacity * 2 newBuckets := make([]*Entry, newCapacity) for i := 0; i < h.capacity; i++ { entry := h.buckets[i] for entry != nil { next := entry.Next newIndex := h.hash(entry.Key) % newCapacity // 重新計(jì)算索引 entry.Next = newBuckets[newIndex] // 插入新桶頭部 newBuckets[newIndex] = entry entry = next } } h.buckets = newBuckets h.capacity = newCapacity } func main() { hm := NewHashMap(2) // 初始容量設(shè)為2便于觸發(fā)擴(kuò)容 hm.Put("name", "Alice") hm.Put("age", 30) hm.Put("lang", "Go") // 觸發(fā)擴(kuò)容 if val, ok := hm.Get("name"); ok { fmt.Println("name:", val) // 輸出 Alice } hm.Delete("age") if _, ok := hm.Get("age"); !ok { fmt.Println("age deleted") // 輸出此句 } }
到此這篇關(guān)于Golang HashMap實(shí)現(xiàn)原理的文章就介紹到這了,更多相關(guān)goland hashmap原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言中命令行參數(shù)解析工具pflag的使用指南
在使用?Go?進(jìn)行開(kāi)發(fā)的過(guò)程中,命令行參數(shù)解析是我們經(jīng)常遇到的需求,于是?Go?社區(qū)中出現(xiàn)了一個(gè)叫?pflag?的第三方包,功能更加全面且足夠強(qiáng)大,下面我們就來(lái)看看它的具體使用吧2024-11-11深入了解Golang網(wǎng)絡(luò)編程N(yùn)et包的使用
net包主要是增加?context?控制,封裝了一些不同的連接類(lèi)型以及DNS?查找等等,同時(shí)在有需要的地方引入?goroutine?提高處理效率。本文主要和大家分享下在Go中網(wǎng)絡(luò)編程的實(shí)現(xiàn),需要的可以參考一下2022-07-07GoLang中的timer定時(shí)器實(shí)現(xiàn)原理分析
Timer中對(duì)外暴露的只有一個(gè)channel,這個(gè) channel也是定時(shí)器的核心。當(dāng)計(jì)時(shí)結(jié)束時(shí),Timer會(huì)發(fā)送值到channel中,外部環(huán)境在這個(gè) channel 收到值的時(shí)候,就代表計(jì)時(shí)器超時(shí)了,可與select搭配執(zhí)行一些超時(shí)邏輯2023-02-02golang中select語(yǔ)句的簡(jiǎn)單實(shí)例
Go的select語(yǔ)句是一種僅能用于channl發(fā)送和接收消息的專(zhuān)用語(yǔ)句,此語(yǔ)句運(yùn)行期間是阻塞的,下面這篇文章主要給大家介紹了關(guān)于golang中select語(yǔ)句的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用過(guò)程解析
這篇文章主要介紹了golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用,包括RPC具體實(shí)現(xiàn)結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Golang?channel底層實(shí)現(xiàn)過(guò)程解析(深度好文)
Go語(yǔ)言為了方便使用者,提供了簡(jiǎn)單、安全的協(xié)程數(shù)據(jù)同步和通信機(jī)制,這篇文章主要介紹了Golang?channel底層是如何實(shí)現(xiàn)的,需要的朋友可以參考下2024-07-07簡(jiǎn)單聊聊為什么說(shuō)Go語(yǔ)言字符串是不可變的
最近有讀者留言說(shuō),平時(shí)在寫(xiě)代碼的過(guò)程中,是會(huì)對(duì)字符串進(jìn)行修改的,但網(wǎng)上都說(shuō) Go 語(yǔ)言字符串是不可變的,這是為什么呢,本文就來(lái)和大家簡(jiǎn)單講講2023-05-05