参考链接:
https://www.cnblogs.com/jing99/p/11736005.html
复杂度
TreeMap的基本操作 containsKey、get、put 和 remove 的时间复杂度是 log(n)。
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
继承的类和接口都是老熟人了,就是这个NavigableMap有点陌生,我们就来康康
public interface NavigableMap<K,V> extends SortedMap<K,V>{
/**
* Returns a key-value mapping associated with the greatest key
* strictly less than the given key, or {@code null} if there is
* no such key.
* 返回严格小于给定的键的最大键关联的键值映射,或者{@code null}(如果没
* 有这样的key
* @param key the key
* @参数 key 传入的键
* @return an entry with the greatest key less than {@code key},
* or {@code null} if there is no such key
* 返回一个小于{@code key}的有最大键的entry,或{@code null},如果没有这样的key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* 指定的key无法被比较将抛出异常
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
* 指定的key为null,同时该map不允许null key将抛出异常
*/
底层数据结构:红黑树(是一种平衡二叉树,也是一种二叉搜索树)
平衡二叉树:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树
特点:
查找、插入、删除复杂度均为log(n)