type Iterator

  1. func (iterator *Iterator) Begin() // 将迭代器重置为初始状态,然后调用Next获取第一个元素
  2. func (iterator *Iterator) End() // 将迭代器移过最后一个元素,然后调用Prev获取最后一个元素
  3. // 将迭代器移动到第一个元素,如果容器中有第一个元素则返回true。
  4. // 如果First()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态
  5. func (iterator *Iterator) First() bool
  6. // 将迭代器移动到最后元素,如果容器中有第一个元素则返回true。
  7. // 如果Last()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态
  8. func (iterator *Iterator) Last() bool
  9. func (iterator *Iterator) Next() bool
  10. func (iterator *Iterator) Prev() bool
  11. func (iterator *Iterator) Index() int
  12. func (iterator *Iterator) Value() interface{}

type List

  1. func New(values ...interface{}) *List
  2. func (list *List) Add(values ...interface{})
  3. func (list *List) All(f func(index int, value interface{}) bool) bool
  4. func (list *List) Any(f func(index int, value interface{}) bool) bool
  5. func (list *List) Append(values ...interface{})
  6. func (list *List) Clear()
  7. func (list *List) Contains(values ...interface{}) bool
  8. func (list *List) Each(f func(index int, value interface{}))
  9. func (list *List) Empty() bool
  10. // 将容器中的每个元素传递给给定的函数,并返回函数为true或-1的第一个元素(索引,值)
  11. // 如果没有元素符合条件,则返回nil
  12. func (list *List) Find(f func(index int, value interface{}) bool) (index int, value interface{})
  13. func (list *List) FromJSON(data []byte) error
  14. func (list *List) Get(index int) (interface{}, bool)
  15. func (list *List) IndexOf(value interface{}) int
  16. func (list *List) Insert(index int, values ...interface{})
  17. func (list *List) Iterator() Iterator
  18. // 为每个元素调用给定函数一次,并返回一个包含给定函数返回值的容器
  19. func (list *List) Map(f func(index int, value interface{}) interface{}) *List
  20. // 在首部添加元素
  21. func (list *List) Prepend(values ...interface{})
  22. func (list *List) Remove(index int)
  23. // 返回一个新容器,其中包含给定函数返回真值的所有元素
  24. func (list *List) Select(f func(index int, value interface{}) bool) *List
  25. func (list *List) Set(index int, value interface{})
  26. func (list *List) Size() int
  27. func (list *List) Sort(comparator utils.Comparator)
  28. func (list *List) String() string
  29. func (list *List) Swap(i, j int)
  30. func (list *List) ToJSON() ([]byte, error)
  31. func (list *List) Values() []interface{}

例子

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