Java集合-TreeMap源码

桃扇骨 2022-04-18 06:18 347阅读 0赞

数据结构

  • 红黑树
  • 实现了NavigableMap,是一个key有序的Map

源码

成员变量

  1. private final Comparator<? super K> comparator;
  2. private transient Entry<K,V> root;
  3. /** * The number of entries in the tree */
  4. private transient int size = 0;
  5. /** * The number of structural modifications to the tree. */
  6. private transient int modCount = 0;

构造函数

  1. public TreeMap() {
  2. comparator = null;
  3. }
  4. public TreeMap(Comparator<? super K> comparator) {
  5. this.comparator = comparator;
  6. }
  7. public TreeMap(Map<? extends K, ? extends V> m) {
  8. comparator = null;
  9. putAll(m);
  10. }
  11. /** * Constructs a new tree map containing the same mappings and * using the same ordering as the specified sorted map. This * method runs in linear time. */
  12. public TreeMap(SortedMap<K, ? extends V> m) {
  13. comparator = m.comparator();
  14. try {
  15. buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
  16. } catch (java.io.IOException cannotHappen) {
  17. } catch (ClassNotFoundException cannotHappen) {
  18. }
  19. }

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java分析05-TreeMap

    说什么王权富贵,坚持!      今天,我们来看下TreeMap集合。作为Map集合中的一员大将,她的职责还是很大的, 除了常见的存储键值对和快速查找,她还有很多技能,例如

    相关 Java解析》TreeMap

    红黑树顾名思义就是节点是红色或者黑色的平衡二叉树,它通过颜色的约束来维持着二叉树的平衡。对于一棵有效的红黑树二叉树而言我们必须增加如下规则: 1、每个节点都只能是红色或者黑