From 551e66ba29f37f2653a082c4ce3a3aedf4e00cfb Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 24 Aug 2022 10:20:02 +0800 Subject: [PATCH] refactor: update HashMap Put method --- datastructure/hashmap/hashmap.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/datastructure/hashmap/hashmap.go b/datastructure/hashmap/hashmap.go index 6950444..12fd9b5 100644 --- a/datastructure/hashmap/hashmap.go +++ b/datastructure/hashmap/hashmap.go @@ -53,11 +53,11 @@ func (hm *HashMap) Get(key any) any { } // Put new key value in hashmap -func (hm *HashMap) Put(key any, value any) any { - return hm.putValue(hm.hash(key), key, value) +func (hm *HashMap) Put(key any, value any) { + hm.putValue(hm.hash(key), key, value) } -func (hm *HashMap) putValue(hash uint64, key, value any) any { +func (hm *HashMap) putValue(hash uint64, key, value any) { if hm.capacity == 0 { hm.capacity = defaultMapCapacity hm.table = make([]*mapNode, defaultMapCapacity) @@ -68,14 +68,11 @@ func (hm *HashMap) putValue(hash uint64, key, value any) any { hm.table[hash] = newMapNode(key, value) } else if node.key == key { hm.table[hash] = newMapNodeWithNext(key, value, node) - return value } else { hm.resize() - return hm.putValue(hash, value, value) + hm.putValue(hash, value, value) } hm.size++ - - return value } // Delete item by given key in hashmap