TreeMap

作者: ae12 | 来源:发表于2018-09-14 14:36 被阅读5次

A Red-Black Tree based NevigableMap implementation. the map is sorted according to the natural ordering of the key or by a Comparator provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed **log(n) time cost **for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest’s Introduction to Algorithms.

hashMap 不保证数据有序,而LInkedHashMap保证数据可以保持插入顺序
而如果希望 map 按照Key大小保持顺序,可以用TreeMap

如果我们希望Map可以保持key的大小顺序的时候,我们就需要利用TreeMap了。

TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();
tmap.put(1, "语文");
tmap.put(3, "英语");
tmap.put(2, "数学");
tmap.put(4, "政治");
tmap.put(5, "历史");
tmap.put(6, "地理");
tmap.put(7, "生物");
tmap.put(8, "化学");
for(Entry<Integer, String> entry : tmap.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

结构图


treeMap.png

相关文章

  • TreeMap了解一下

    前言 TreeMap TreeMap类继承图 TreeMap的域 TreeMap的构造函数 TreeMap常见Ap...

  • TreeMap

    还是从几个常用的方法如数: TreeMap.TreeMap() : TreeMap.put() :   目前不清楚...

  • TreeMap及Set源码解析

    1、本文主要内容 TreeMap及Set介绍 TreeMap源码解析 Set源码解析 2、TreeMap及Set介...

  • lambda HashMap 排序

    TreeMap 按key排序生成map可以有TreeMap 完成,TreeMap可以按key的自然顺序排序(Com...

  • Java集合TreeMap用法总结

    Java的TreeMap是集合框架中的一个实现类,TreeMap继承了AbstractMap。TreeMap实现了...

  • java8中treemap源码分析

    分析大纲: treemap中的实现原理 treemap中的remove()(红黑树的删除实践) treemap中的...

  • Java集合类-TreeMap

    TreeMap和HashMap的区别和共同点 TreeMap 简介 TreeMap 是一个有序的key-value...

  • TreeMap简介

    TreeMap是支持排序的map,基于红黑树,无容量限制,TreeMap非线程安全。 TreeMap继承Abstr...

  • python pyecharts绘制矩形树图Treemap

    @[toc] treemap_base treemap_levels echarts_option_query

  • JDK源码解析——TreeMap

    第1部分 TreeMap介绍 TreeMap 简介 TreeMap 是一个有序的key-value集合,它是通过红...

网友评论

      本文标题:TreeMap

      本文链接:https://www.haomeiwen.com/subject/ntxwgftx.html