Set
Set is unordered and implements the ICollection interface. It represents a collection of unique elements.
Example
Basic
The basic usage is very similar to List because they both implement ICollection
package main
import (
"fmt"
"github.com/KafkaWannaFly/generic-collections/list"
"github.com/KafkaWannaFly/generic-collections/set"
)
func main() {
integerSet := set.From(1, 2, 3, 4, 5)
// Loop through the set
// Set is unordered, so the index is always 0
// The order of the elements is not guaranteed
integerSet.ForEach(func(index int, item int) {
fmt.Printf("Index: %d, Item: %d\n", index, item)
})
// Output: 1 2 3 4 5 6 7 8
// 1 2 3 4 5 are already in the set
integerSet.Add(1).Add(2).Add(3).Add(4).Add(5).Add(6).Add(7).Add(8)
integerSet.AddAll(
set.From(9, 10, 11, 12, 13, 14, 15),
)
fmt.Printf("Assert integer set %t\n", set.IsSet[int](integerSet)) // true
fmt.Printf("Assert string set %t\n", set.IsSet[string](integerSet)) // false
fmt.Printf("Contains 1: %t\n", integerSet.Has(1)) // true
fmt.Printf("Contains 100: %t\n", integerSet.Has(100)) // false
fmt.Printf(
"Contains all 1, 2, 3, 4, 5: %t\n",
integerSet.HasAll(list.From(1, 2, 3, 4, 5)),
) // true
}Set Specific Methods

Last updated