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

例子

  1. package main
  2. import (
  3. "github.com/emirpasic/gods/lists/arraylist"
  4. "github.com/emirpasic/gods/utils"
  5. )
  6. // ArrayListExample to demonstrate basic usage of ArrayList
  7. func main() {
  8. list := arraylist.New()
  9. list.Add("a") // ["a"]
  10. list.Add("c", "b") // ["a","c","b"]
  11. list.Sort(utils.StringComparator) // ["a","b","c"]
  12. _, _ = list.Get(0) // "a",true
  13. _, _ = list.Get(100) // nil,false
  14. _ = list.Contains("a", "b", "c") // true
  15. _ = list.Contains("a", "b", "c", "d") // false
  16. list.Swap(0, 1) // ["b","a",c"]
  17. list.Remove(2) // ["b","a"]
  18. list.Remove(1) // ["b"]
  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. }