Treemap实现了一个红黑树支持的映射。
元素在映射中按键排序
type Iterator
func (iterator *Iterator) Begin() // 将迭代器重置为初始状态,然后调用Next获取第一个元素func (iterator *Iterator) End() // 将迭代器移过最后一个元素,然后调用Prev获取最后一个元素// 将迭代器移动到第一个元素,如果容器中有第一个元素则返回true。// 如果First()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态func (iterator *Iterator) First() bool// 将迭代器移动到最后元素,如果容器中有第一个元素则返回true。// 如果Last()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态func (iterator *Iterator) Last() boolfunc (iterator *Iterator) Next() boolfunc (iterator *Iterator) Prev() boolfunc (iterator *Iterator) Index() intfunc (iterator *Iterator) Value() interface{}
type Map
func NewWith(comparator utils.Comparator) *Map// 实例化一个具有IntComparator的树映射,即键是int类型的func NewWithIntComparator() *Map// 用StringComparator实例化一个树映射,即键的类型是stringfunc NewWithStringComparator() *Map// 将集合中的每个元素传递给给定的函数,如果函数对所有元素都返回true,则返回truefunc (m *Map) All(f func(key interface{}, value interface{}) bool) bool// 将集合中的每个元素传递给给定的函数,如果函数对任何元素返回true,则返回truefunc (m *Map) Any(f func(key interface{}, value interface{}) bool) boolfunc (m *Map) Ceiling(key interface{}) (foundKey interface{}, foundValue interface{})func (m *Map) Clear()func (m *Map) Each(f func(key interface{}, value interface{}))func (m *Map) Empty() bool// 将容器中的每个元素传递给给定的函数,并返回函数为true或-1的第一个元素(索引,值)// 如果没有元素符合条件,则返回nilfunc (m *Map) Find(f func(key interface{}, value interface{}) bool) (interface{}, interface{})func (m *Map) Floor(key interface{}) (foundKey interface{}, foundValue interface{})func (m *Map) FromJSON(data []byte) errorfunc (m *Map) Get(key interface{}) (value interface{}, found bool)func (m *Map) Iterator() Iteratorfunc (m *Map) Keys() []interface{}// 为每个元素调用给定函数一次,并返回一个包含给定函数返回值的容器func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Mapfunc (m *Map) Max() (key interface{}, value interface{})func (m *Map) Min() (key interface{}, value interface{})func (m *Map) Put(key interface{}, value interface{})func (m *Map) Remove(key interface{})// 返回一个新容器,其中包含给定函数返回真值的所有元素func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Mapfunc (m *Map) Size() intfunc (m *Map) String() stringfunc (m *Map) ToJSON() ([]byte, error)func (m *Map) Values() []interface{}
例子
package mainimport "github.com/emirpasic/gods/maps/treemap"// TreeMapExample to demonstrate basic usage of TreeMapfunc main() {m := treemap.NewWithIntComparator() // empty (keys are of type int)m.Put(1, "x") // 1->xm.Put(2, "b") // 1->x, 2->b (in order)m.Put(1, "a") // 1->a, 2->b (in order)_, _ = m.Get(2) // b, true_, _ = m.Get(3) // nil, false_ = m.Values() // []interface {}{"a", "b"} (in order)_ = m.Keys() // []interface {}{1, 2} (in order)m.Remove(1) // 2->bm.Clear() // emptym.Empty() // truem.Size() // 0}
