Hashmap: Add FilterByValue (#184)

The FilterByValue function is a method defined on the HashMap type. It generates a new HashMap
containing only the elements that satisfy a specified condition,
as determined by a predicate function applied to each element's value.

Note: Will add later doc
This commit is contained in:
donutloop
2024-02-27 03:36:26 +01:00
committed by GitHub
parent 4a539a23c8
commit 069812e0ee
2 changed files with 45 additions and 0 deletions

View File

@@ -105,3 +105,24 @@ func TestHashMap_GetOrDefault(t *testing.T) {
assert.Equal(1, hm.GetOrDefault("a", 5))
assert.Equal(5, hm.GetOrDefault("d", 5))
}
func TestHashMap_FilterByValue(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHashMap_FilterByValue")
hm := NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Put("d", 4)
hm.Put("e", 5)
hm.Put("f", 6)
filteredHM := hm.FilterByValue(func(value any) bool {
return value.(int) == 1 || value.(int) == 3
})
assert.Equal(uint64(2), filteredHM.Size())
}