Lists

A list is a data structure that stores values and may have repeated values.

Implements Container interface.

  1. type List interface {
  2. Get(index int) (interface{}, bool)
  3. Remove(index int)
  4. Add(values ...interface{})
  5. Contains(values ...interface{}) bool
  6. Sort(comparator utils.Comparator)
  7. Swap(index1, index2 int)
  8. Insert(index int, values ...interface{})
  9. Set(index int, value interface{})
  10. containers.Container
  11. // Empty() bool
  12. // Size() int
  13. // Clear()
  14. // Values() []interface{}
  15. }

ArrayList

A list backed by a dynamic array that grows and shrinks implicitly.

Implements List, IteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import (
  3. "github.com/emirpasic/gods/lists/arraylist"
  4. "github.com/emirpasic/gods/utils"
  5. )
  6. func main() {
  7. list := arraylist.New()
  8. list.Add("a") // ["a"]
  9. list.Add("c", "b") // ["a","c","b"]
  10. list.Sort(utils.StringComparator) // ["a","b","c"]
  11. _, _ = list.Get(0) // "a",true
  12. _, _ = list.Get(100) // nil,false
  13. _ = list.Contains("a", "b", "c") // true
  14. _ = list.Contains("a", "b", "c", "d") // false
  15. list.Swap(0, 1) // ["b","a",c"]
  16. list.Remove(2) // ["b","a"]
  17. list.Remove(1) // ["b"]
  18. list.Remove(0) // []
  19. list.Remove(0) // [] (ignored)
  20. _ = list.Empty() // true
  21. _ = list.Size() // 0
  22. list.Add("a") // ["a"]
  23. list.Clear() // []
  24. list.Insert(0, "b") // ["b"]
  25. list.Insert(0, "a") // ["a","b"]
  26. }

SinglyLinkedList

A list where each element points to the next element in the list.

Implements List, IteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import (
  3. sll "github.com/emirpasic/gods/lists/singlylinkedlist"
  4. "github.com/emirpasic/gods/utils"
  5. )
  6. func main() {
  7. list := sll.New()
  8. list.Add("a") // ["a"]
  9. list.Add("c", "b") // ["a","c","b"]
  10. list.Sort(utils.StringComparator) // ["a","b","c"]
  11. _, _ = list.Get(0) // "a",true
  12. _, _ = list.Get(100) // nil,false
  13. _ = list.Contains("a", "b", "c") // true
  14. _ = list.Contains("a", "b", "c", "d") // false
  15. list.Swap(0, 1) // ["b","a",c"]
  16. list.Remove(2) // ["b","a"]
  17. list.Remove(1) // ["b"]
  18. list.Remove(0) // []
  19. list.Remove(0) // [] (ignored)
  20. _ = list.Empty() // true
  21. _ = list.Size() // 0
  22. list.Add("a") // ["a"]
  23. list.Clear() // []
  24. list.Insert(0, "b") // ["b"]
  25. list.Insert(0, "a") // ["a","b"]
  26. }

DoublyLinkedList

A list where each element points to the next and previous elements in the list.

Implements List, IteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import (
  3. dll "github.com/emirpasic/gods/lists/doublylinkedlist"
  4. "github.com/emirpasic/gods/utils"
  5. )
  6. func main() {
  7. list := dll.New()
  8. list.Add("a") // ["a"]
  9. list.Add("c", "b") // ["a","c","b"]
  10. list.Sort(utils.StringComparator) // ["a","b","c"]
  11. _, _ = list.Get(0) // "a",true
  12. _, _ = list.Get(100) // nil,false
  13. _ = list.Contains("a", "b", "c") // true
  14. _ = list.Contains("a", "b", "c", "d") // false
  15. list.Swap(0, 1) // ["b","a",c"]
  16. list.Remove(2) // ["b","a"]
  17. list.Remove(1) // ["b"]
  18. list.Remove(0) // []
  19. list.Remove(0) // [] (ignored)
  20. _ = list.Empty() // true
  21. _ = list.Size() // 0
  22. list.Add("a") // ["a"]
  23. list.Clear() // []
  24. list.Insert(0, "b") // ["b"]
  25. list.Insert(0, "a") // ["a","b"]
  26. }