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() bool
func (iterator *Iterator) Next() bool
func (iterator *Iterator) Prev() bool
func (iterator *Iterator) Index() int
func (iterator *Iterator) Value() interface{}
type Map
func NewWith(comparator utils.Comparator) *Map
// 实例化一个具有IntComparator的树映射,即键是int类型的
func NewWithIntComparator() *Map
// 用StringComparator实例化一个树映射,即键的类型是string
func NewWithStringComparator() *Map
// 将集合中的每个元素传递给给定的函数,如果函数对所有元素都返回true,则返回true
func (m *Map) All(f func(key interface{}, value interface{}) bool) bool
// 将集合中的每个元素传递给给定的函数,如果函数对任何元素返回true,则返回true
func (m *Map) Any(f func(key interface{}, value interface{}) bool) bool
func (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的第一个元素(索引,值)
// 如果没有元素符合条件,则返回nil
func (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) error
func (m *Map) Get(key interface{}) (value interface{}, found bool)
func (m *Map) Iterator() Iterator
func (m *Map) Keys() []interface{}
// 为每个元素调用给定函数一次,并返回一个包含给定函数返回值的容器
func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Map
func (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) *Map
func (m *Map) Size() int
func (m *Map) String() string
func (m *Map) ToJSON() ([]byte, error)
func (m *Map) Values() []interface{}
例子
package main
import "github.com/emirpasic/gods/maps/treemap"
// TreeMapExample to demonstrate basic usage of TreeMap
func main() {
m := treemap.NewWithIntComparator() // empty (keys are of type int)
m.Put(1, "x") // 1->x
m.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->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}