TreeMap理解

TreeMap

结构

public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable

特点

  • 基于红黑树实现
  • 线程不安全
  • 可插入null
  • 如果集合没有指定的比较器则key必须实现Comparable接口

NavigableMap接口
提供按升序或降序键顺序访问和遍历

public interface NavigableMap<K,V> extends SortedMap<K,V> {
	//返回小于key的第一个元素
	Map.Entry<K,V> lowerEntry(K key);
	//返回小于key的第一个键
	K lowerKey(K key);
	//返回小于等于key的第一个元素
	Map.Entry<K,V> floorEntry(K key);
	//返回与大于或等于给定键的最小键相关联的键值映射,如果没有此键,则 null 。 
	Map.Entry<K,V> ceilingEntry(K key);
	//返回大于key的第一个元素
	Map.Entry<K,V> higherEntry(K key);
	//返回集合中第一个元素
	Map.Entry<K,V> firstEntry();
	//返回集合中最后一个元素
	Map.Entry<K,V> lastEntry();
	//返回集合中第一个元素,并从集合中删除
	Map.Entry<K,V> pollFirstEntry();
	//返回集合中最后一个元素,并从集合中删除
	Map.Entry<K,V> pollLastEntry();
	//返回倒序的Map集合
	NavigableMap<K,V> descendingMap();
}

成员变量

//节点集合
private transient EntrySet entrySet;
//键集合
private transient KeySet<K> navigableKeySet;
//倒序map集合
private transient NavigableMap<K,V> descendingMap;
//比较器
private final Comparator<? super K> comparator;
//树的根节点
private transient Entry<K,V> root;
内部类

Entry
TreeMap中的树节点

static final class Entry<K,V> implements Map.Entry<K,V> {
     K key;
     V value;
     Entry<K,V> left;
     Entry<K,V> right;
     Entry<K,V> parent;
     boolean color = BLACK;
}    

PrivateEntryIterator
TreeMap的基本迭代器

abstract class PrivateEntryIterator<T> implements Iterator<T> {
    Entry<K,V> next;
    Entry<K,V> lastReturned;
    int expectedModCount;

    //从指定头节点开始遍历
    PrivateEntryIterator(Entry<K,V> first) {
        expectedModCount = modCount;
        lastReturned = null;
        next = first;
    }

    public final boolean hasNext() {
        return next != null;
    }

    final Entry<K,V> nextEntry() {
        Entry<K,V> e = next;
        if (e == null)
            throw new NoSuchElementException();
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        //获得后继节点
        next = successor(e);
        lastReturned = e;
        return e;
    }

    final Entry<K,V> prevEntry() {
        Entry<K,V> e = next;
        if (e == null)
            throw new NoSuchElementException();
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        //获得前驱节点
        next = predecessor(e);
        lastReturned = e;
        return e;
    }

    public void remove() {
        if (lastReturned == null)
            throw new IllegalStateException();
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        // 由后继元素替换当前删除元素作为下一次遍历的元素
        if (lastReturned.left != null && lastReturned.right != null)
            next = lastReturned;
        //删除当前节点
        deleteEntry(lastReturned);
        expectedModCount = modCount;
        lastReturned = null;
    }
}

重要方法
只分析TreeMap的遍历方法,对于插入和删除方法都是基于红黑树,理解了红黑树自然就能理解。
get(Object key)

public V get(Object key) {
    Entry<K,V> p = getEntry(key);
    return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) {
    // 如果当前TreeMap的比较器不为空则调用比较器来获得key
    if (comparator != null)
        return getEntryUsingComparator(key);
    if (key == null)
        throw new NullPointerException();
    @SuppressWarnings("unchecked")
        //存入TreeMap中的键如果不存在比较器则必须继承Comparable接口,否则抛出ClassCastException
        Comparable<? super K> k = (Comparable<? super K>) key;
    Entry<K,V> p = root;
    //调用Comparable接口中的compareTo方法来判断key是否相等
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0)
            p = p.left;
        else if (cmp > 0)
            p = p.right;
        else
            return p;
    }
    return null;
}
//通过调用比较器的compare方法来查找key
final Entry<K,V> getEntryUsingComparator(Object key) {
    @SuppressWarnings("unchecked")
        K k = (K) key;
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = cpr.compare(k, p.key);
            if (cmp < 0)
                p = p.left;
            else if (cmp > 0)
                p = p.right;
            else
                return p;
        }
    }
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值