Maps

A Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.

Implements Container interface.

  1. type Map interface {
  2. Put(key interface{}, value interface{})
  3. Get(key interface{}) (value interface{}, found bool)
  4. Remove(key interface{})
  5. Keys() []interface{}
  6. containers.Container
  7. // Empty() bool
  8. // Size() int
  9. // Clear()
  10. // Values() []interface{}
  11. }

A BidiMap is an extension to the Map. A bidirectional map (BidiMap), also called a hash bag, is an associative data structure in which the key-value pairs form a one-to-one relation. This relation works in both directions by allow the value to also act as a key to key, e.g. a pair (a,b) thus provides a coupling between ‘a’ and ‘b’ so that ‘b’ can be found when ‘a’ is used as a key and ‘a’ can be found when ‘b’ is used as a key.

  1. type BidiMap interface {
  2. GetKey(value interface{}) (key interface{}, found bool)
  3. Map
  4. }

HashMap

A map based on hash tables. Keys are unordered.

Implements Map, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import "github.com/emirpasic/gods/maps/hashmap"
  3. func main() {
  4. m := hashmap.New() // empty
  5. m.Put(1, "x") // 1->x
  6. m.Put(2, "b") // 2->b, 1->x (random order)
  7. m.Put(1, "a") // 2->b, 1->a (random order)
  8. _, _ = m.Get(2) // b, true
  9. _, _ = m.Get(3) // nil, false
  10. _ = m.Values() // []interface {}{"b", "a"} (random order)
  11. _ = m.Keys() // []interface {}{1, 2} (random order)
  12. m.Remove(1) // 2->b
  13. m.Clear() // empty
  14. m.Empty() // true
  15. m.Size() // 0
  16. }

TreeMap

A map based on red-black tree. Keys are ordered ordered with respect to the comparator.

Implements Map, IteratorWithKey, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import "github.com/emirpasic/gods/maps/treemap"
  3. func main() {
  4. m := treemap.NewWithIntComparator() // empty (keys are of type int)
  5. m.Put(1, "x") // 1->x
  6. m.Put(2, "b") // 1->x, 2->b (in order)
  7. m.Put(1, "a") // 1->a, 2->b (in order)
  8. _, _ = m.Get(2) // b, true
  9. _, _ = m.Get(3) // nil, false
  10. _ = m.Values() // []interface {}{"a", "b"} (in order)
  11. _ = m.Keys() // []interface {}{1, 2} (in order)
  12. m.Remove(1) // 2->b
  13. m.Clear() // empty
  14. m.Empty() // true
  15. m.Size() // 0
  16. // Other:
  17. m.Min() // Returns the minimum key and its value from map.
  18. m.Max() // Returns the maximum key and its value from map.
  19. }

LinkedHashMap

A map that preserves insertion-order. It is backed by a hash table to store values and doubly-linked list to store ordering.

Implements Map, IteratorWithKey, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import "github.com/emirpasic/gods/maps/linkedhashmap"
  3. func main() {
  4. m := linkedhashmap.New() // empty (keys are of type int)
  5. m.Put(2, "b") // 2->b
  6. m.Put(1, "x") // 2->b, 1->x (insertion-order)
  7. m.Put(1, "a") // 2->b, 1->a (insertion-order)
  8. _, _ = m.Get(2) // b, true
  9. _, _ = m.Get(3) // nil, false
  10. _ = m.Values() // []interface {}{"b", "a"} (insertion-order)
  11. _ = m.Keys() // []interface {}{2, 1} (insertion-order)
  12. m.Remove(1) // 2->b
  13. m.Clear() // empty
  14. m.Empty() // true
  15. m.Size() // 0
  16. }

HashBidiMap

A map based on two hashmaps. Keys are unordered.

Implements BidiMap, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import "github.com/emirpasic/gods/maps/hashbidimap"
  3. func main() {
  4. m := hashbidimap.New() // empty
  5. m.Put(1, "x") // 1->x
  6. m.Put(3, "b") // 1->x, 3->b (random order)
  7. m.Put(1, "a") // 1->a, 3->b (random order)
  8. m.Put(2, "b") // 1->a, 2->b (random order)
  9. _, _ = m.GetKey("a") // 1, true
  10. _, _ = m.Get(2) // b, true
  11. _, _ = m.Get(3) // nil, false
  12. _ = m.Values() // []interface {}{"a", "b"} (random order)
  13. _ = m.Keys() // []interface {}{1, 2} (random order)
  14. m.Remove(1) // 2->b
  15. m.Clear() // empty
  16. m.Empty() // true
  17. m.Size() // 0
  18. }

TreeBidiMap

A map based on red-black tree. This map guarantees that the map will be in both ascending key and value order. Other than key and value ordering, the goal with this structure is to avoid duplication of elements (unlike in HashBidiMap), which can be significant if contained elements are large.

Implements BidiMap, IteratorWithKey, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import (
  3. "github.com/emirpasic/gods/maps/treebidimap"
  4. "github.com/emirpasic/gods/utils"
  5. )
  6. func main() {
  7. m := treebidimap.NewWith(utils.IntComparator, utils.StringComparator)
  8. m.Put(1, "x") // 1->x
  9. m.Put(3, "b") // 1->x, 3->b (ordered)
  10. m.Put(1, "a") // 1->a, 3->b (ordered)
  11. m.Put(2, "b") // 1->a, 2->b (ordered)
  12. _, _ = m.GetKey("a") // 1, true
  13. _, _ = m.Get(2) // b, true
  14. _, _ = m.Get(3) // nil, false
  15. _ = m.Values() // []interface {}{"a", "b"} (ordered)
  16. _ = m.Keys() // []interface {}{1, 2} (ordered)
  17. m.Remove(1) // 2->b
  18. m.Clear() // empty
  19. m.Empty() // true
  20. m.Size() // 0
  21. }