转自:
下文讲述TreeMap类的功能简介说明,如下所示:
TreeMap继承了NavigableMap 而NavigableMap继承自SortedMap 为SortedMap添加了搜索选项
TreeMap构造函数
//无参构造函数 TreeMap() // 创建的TreeMap包含Map TreeMap(Map extends K, ? extends V> copyFrom) //设置Tree的比较器 TreeMap(Comparator super K> comparator) //创建的TreeSet包含copyFrom TreeMap(SortedMapcopyFrom)
TreeMap属性
//比较器 由于TreeMap是有序的,可以通过定义外部比较器,为Treemap设置自定义比较规则 private final Comparator super K> comparator; //TreeMap红-黑节点,为TreeMap的内部类 private transient Entryroot = null; //容器大小 private transient int size = 0; //TreeMap修改次数 private transient int modCount = 0; //红黑树的节点颜色--红色 private static final boolean RED = false; //红黑树的节点颜色--黑色 private static final boolean BLACK = true;
TreeMap方法
EntryceilingEntry(K key) K ceilingKey(K key) void clear() Object clone() Comparator super K> comparator() boolean containsKey(Object key) NavigableSet descendingKeySet() NavigableMap descendingMap() Set > entrySet() Entry firstEntry() K firstKey() Entry floorEntry(K key) K floorKey(K key) V get(Object key) NavigableMap headMap(K to, boolean inclusive) SortedMap headMap(K toExclusive) Entry higherEntry(K key) K higherKey(K key) boolean isEmpty() Set keySet() Entry lastEntry() K lastKey() Entry lowerEntry(K key) K lowerKey(K key) NavigableSet navigableKeySet() Entry pollFirstEntry() Entry pollLastEntry() V put(K key, V value) V remove(Object key) int size() SortedMap subMap(K fromInclusive, K toExclusive) NavigableMap subMap(K from, boolean fromInclusive, K to, boolean toInclusive) NavigableMap tailMap(K from, boolean inclusive) SortedMap tailMap(K fromInclusive)