前言

TreeMap用于存储与HashMap类非常相似的键值对。区别在于TreeMap提供了一种以排序顺序存储键/值对的有效方法。它是基于红黑树NavigableMap实现。

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

一、常用methods

  • void clear(): 它从地图中删除所有键值对。

  • int size(): 返回此映射中存在的键值对的数量。

  • boolean containsKey(Object key) : 如果此映射包含指定键的映射,则返回 true

  • boolean containsValue(Object value) : 如果此地图将一个或多个键映射到指定值,则返回 true

  • Set<Map.Entry<K,V>> entrySet( ): 返回此地图中包含的映射的Set视图。常用于遍历。

  • V get(Object key): 返回到指定键所映射的值,若没有则返回null

  • Set KeySet() : 返回此地图中包含的键的Set视图。 常用于遍历map中的Key。

  • V put(K key, V value) : 将指定的值与此映射中的指定键相关联。即将键值对加入map中。

  • V remove(Object key): 删除指定键的键值对。

  • V replace(K key,V value): 替换指定键的值。

  • V replace(K key,V oldValue,V newValue): 替换指定键值对的值。若不存在指定的键值对,则不操作。

二、特有的methods

  • Map.Entry <K,V> ceilingEntry(K key) : 返回最小的大于或等于指定的Key的元素,若没有,则返回null
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.ceilingEntry("2.5");System.out.println(entry+"====="+entry.getKey()+"->"+entry.getValue());entry = treeMap.ceilingEntry("4");System.out.println(entry);

  • K ceilingKey(K key) : 返回最小的大于或等于指定Key的Key,如果没有,则返回null
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Object obj = treeMap.ceilingKey("2.5");System.out.println(obj);obj = treeMap.ceilingEntry("4");System.out.println(obj);

  • NavigableSetdescendingKeySet() : 返回集合的全部Key,并且是逆序的
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Set set = treeMap.descendingKeySet();System.out.println(set);

  • NavigableMap<K,V> descendingMap() : 返回集合,并且是逆序的
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");System.out.println(treeMap);Map map = treeMap.descendingMap();System.out.println(map);

  • Map.Entry<K,V> firstEntry() : 返回集合中含有最小的Key的元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.firstEntry();System.out.println(entry);

  • K firstKey() : 返回集合中最小的Key
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Object obj = treeMap.firstKey();System.out.println(obj);

  • Map.Entry<K,V> floorEntry(K key) : 与ceilingEntry()方法相反,返回最大的小于等于指定Key的元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.floorEntry("2.5");System.out.println(entry);

  • K floorKey(K key) : 返回最大的小于等于指定Key的Key
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Object obj = treeMap.floorKey("2.5");System.out.println(obj);

  • SortedMap<K,V> headMap(K Key) : 返回小于指定Key的所有元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Map map = treeMap.headMap("2.5");System.out.println(map);

  • NavigableMap<K,V> headMap(K Key,boolean inclusive) : 当inclusive为true时,返回Key小于等于指定Key的所有元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Map map = treeMap.headMap("2",true);System.out.println(map);map = treeMap.headMap("2",false);System.out.println(map);

  • Map.Entry<K,V> higherEntry(K key) : 返回大于指定Key的所有元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.higherEntry("2.5");System.out.println(entry);

  • K higherKey(K Key) : 返回大于指定Key的所有Key
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Object obj = treeMap.higherKey("2.5");System.out.println(obj);

  • Map.Entry<K,V> lastEntry() : 返回Key最大的元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.lastEntry();System.out.println(entry);

  • K lastKey() : 返回最大的Key
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Object obj = treeMap.lastKey();System.out.println(obj);

  • Map.Entry<K,V> lowerEntry(K Key) : 返回小于指定key的最大元素
         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");Entry entry = treeMap.lowerEntry("2.5");System.out.println(entry);

  • K lowerKey(K Key) : 返回小于指定Key的最大的Key

  • Map.Entry<K,V> pollFirstEntry() : 删除Key最小的元素

         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");treeMap.pollFirstEntry();System.out.println(treeMap);

  • Map.Entry<K,V> pollLastEntry() : 删除Key最大的元素

  • NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) : 截取集合中Key从fromKey到toKey的元素,是否截取他们本身,取决于true或者false

         TreeMap treeMap = new TreeMap();treeMap.put("1", "demo1");treeMap.put("2", "demo2");treeMap.put("3", "demo3");treeMap.put("4", "demo4");Map map = treeMap.subMap("1",true, "4", true);System.out.println(map);map = treeMap.subMap("1",false, "4", true);System.out.println(map);map = treeMap.subMap("1",false, "4", false);System.out.println(map);

  • SortedMap<K,V> subMap(K fromKey, K toKey) : 截取集合中Key从fromKey到toKey的元素,包括fromKey,不包括toKey

  • SortedMap<K,V> tailMap(K fromKey) : 截取Key大于等于fromKey的所有元素

  • NavigableMap<K,V> tailMap(K fromKey, boolean fromInclusive) : 当inclusive为true时,截取Key大于等于fromKey的所有元素,否则截取Key大于fromKey的所有元素

结尾

本文到这里就结束了,感谢看到最后的朋友,都看到最后了,点个赞再走啊,如有不对之处还请多多指正。

天气转凉除了衣服要多穿,知识也要多多积累啊 Java——TreeMap常用methods,还不赶紧收藏起来相关推荐

  1. 天凉了,大家多穿衣服

    这两天天气转凉,我还穿夏天的衬衫,结果今晚回来发现喉咙不舒服,只好去买药了. 大家要保重身体呀!

  2. python发微信提醒天气冷了注意保暖_天气转凉的微信问候语 寒潮来临注意保暖...

    濒临深秋,天气逐渐转凉了,你有没有什么话想对亲近的人说,让他体会一下你的关心.关于天气转凉的微信问候语有哪些你知道吗?一起来看看吧! 1.一周工作,相当繁忙,起早贪黑,经常加班.周末到来,为之疯狂,睡 ...

  3. 天气转晴东方木开始学习IT知识学习篇之一

    吃了一些红由木果后,刚出去转了一下回来,开始写天气转晴东方木开始学习IT知识学习篇之一,春节的时间就这样开始找回来. 老木IT的APP分享的文字,我觉得老木推荐的一键PE启动有用 1.一键操作 操作简 ...

  4. 每日一皮:天气转凉了,你的长袖穿起来了吗?

    往期推荐 每日一皮:好像有个Bug... 你看到了吗? 每日一皮:在调试时,将断点设置在错误的位置... 每日一皮:当我在重构时,总是有惊喜出现....... 每日一皮:周末到了,我以为我能休息一下 ...

  5. qcustomplot删除一条曲线_微凉秋日的成熟风穿搭,选一条V领连衣裙搭配,优雅知性显身材...

    简单时尚的穿搭方式,总是在潜移默化地影响着我们的日常生活.就像是在这微凉的秋季,随着气温逐渐转凉,大家开始将夏日的短袖,换成了长袖,将轻薄舒适的薄纱裙,换成了温柔舒适的针织或丝绒长裙.似乎这所有的改变 ...

  6. 明日立秋 autumn begins,天气渐凉

    一到立秋,梧桐树就开始落叶,由此而来的一种说法就是"一叶知秋".当太阳直射点到达天文经度135度的时候,夏季结束,天气开始转凉.人们可以感觉到风逐渐变冷,早上会出现雾,同时也到了丰 ...

  7. 天气转凉了,程序员都爱格子衫

    传说,互联网上有三大杀器:处女座.五仁月饼和格子衬衫,只要集齐它们就可以破碎山河撕裂人伦混一宇内纵横四海. 西雅图IT圈:seattleit [今日作者]宇直 宇宙第一直男 不开玩笑 在网上 格子衬衫 ...

  8. 【IPv6+燎原系列—第7期】天气渐凉,IPv6+将如何助力天气预报?

    "东边日出西边雨,道是无晴却有晴",天气经常"喜怒无常",充满了不确定性.以前"半夜闻鸡闹,是个风雨兆""乌鸦成群叫,短期风雨到& ...

  9. 气温骤降、天气转凉——一首自己写的歌

    昨晚,站在阳台,望着外面飘着细雨,握着笛子若有所思-- 长沙降温了,昨天还是夏天,今天转眼变成了冬天. 映入眼帘的一切仿佛都不得不用冷字形容,可它们也曾经如烈火一般炽热. 我拿起笛子,即兴演奏出了一段 ...

最新文章

  1. iOS标准库中常用数据结构和算法之内存池
  2. 加载Hadoop+spark镜像文件需要修改的配置文件
  3. opencv提取Mat中的某些行和列
  4. VS编译提示错误“....Consider using strcat_s instead.To disable deprecation, use _CRT_SECURE_NO_WARNINGS.”
  5. I.Mx6 使用串口连接PSAM卡的注意事项
  6. 计算机网络学习笔记(1. 什么是计算机网络?)
  7. PCM data flow - 1 - Overview
  8. js获取数组中的最大值和最小值的方法汇总
  9. poi 拆分带图片的word_POI导出简单的带有图片的Word文档
  10. 微信自动选择浏览器打开方式
  11. matlab机器人工具箱puma560模型学习
  12. DroidCam通过网络调用手机摄像头的方法二
  13. 【Swift】401状态处理流程
  14. (三)bossGroup, workGroup
  15. VUE filters 使用
  16. 姜小白的python日记Day3 初识模块与数据运算
  17. IllegalArgumentException :argument type mismatch
  18. java数组相似度_Java 计算两个字符串的相似度
  19. JavaScript 虚拟键盘:Mindfusion JavaScript Keyboard
  20. 哔咔服务器无响应,哔咔哔咔漫画进不去怎么办 无法进入解决办法

热门文章

  1. 游戏设计自学记录(9)
  2. 探秘最新Linux内核中的自旋锁
  3. python动态生成数据库表_Python版的数据库查询构造器、ORM及动态迁移数据表。
  4. origin三维散点图_OriginPro2017绘制四维散点图(三维散点+一维颜色)
  5. java swing 空白_java – 如何消除BorderLayout.CENTER占用的空白?
  6. Bagging算法最全解析-机器学习
  7. Nginx安装以及详解
  8. 【bzoj4431】[Nwerc2015]Hole in One一杆进洞
  9. VUE父子组件传值(含实例)
  10. linux的rwx权限,linux权限管理:rwx