红黑树在HashMap中的应用
我们知道在jdk1.8之前,HashMap采用链表的方式解决冲突,不过在更新1.8版本之后,
HashMap采用了链表加红黑树的方式来优化了结构。
话不多说,我们来看源码:
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;
结点数量达到8个将链表转为红黑树
/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
static final int UNTREEIFY_THRESHOLD = 6;
resi