NavigableMap与NavigableSet 2021-06-24 14:37 425阅读 0赞 Java集合框架(JavaCollections Framework)加入了一个新的NavigableMap和NavigableSet**接口**——可导航的Map和集合。分别的扩展了SortedMap和SortedSet接口,本质上添加了搜索选项到接口。 对于NavigableMap,有3类方法。 1)用于获取子Map: 主要有 * headMap:获取前面的Map * tailMap:获取后面的Map * subMap:截取子Map public static voidtest1() \{ NavigableMap<Integer,String>navigableMap = new TreeMap<>(); navigableMap.put(5,"5"); navigableMap.put(32,"33"); navigableMap.put(13,"13"); navigableMap.put(1,"1"); NavigableMap<Integer, String> headMap =navigableMap.headMap(5, true);//返回\{1=1, 5=5\} System.out.println(headMap); NavigableMap<Integer,String> tailMap = navigableMap.tailMap(5, true);//\{5=5, 13=13, 32=33\} System.out.println(tailMap); NavigableMap<Integer,String> subMap = navigableMap.subMap(5, true, 13, true);//\{5=5, 13=13\} System.out.println(subMap); \} **说明**:感官上,这些方法与SortedMap的 headMap()、tailMap()和subMap()方法相同,但是返回的类型不一样,均返回上面提到的NavigableMap。 2)用于获取Map Entity: * ceilingEntry(key):用于获取大于或等于给定key的第一个实体,如果没有则返回null。 * firstEntry():用于获取Map的第一个实体,如果没有则返回null。 * floorEntry(key):用于获取小于或等于给定的第一个实体key,如果没有则返回null。 * higherEntry():用于获取大于给定的key的第一个实体,如果没有则返回null。 * lastEntry():用于获取Map最后一个实体,如果没有则返回null。 * lowerEntry(key):用于获取小于给定key的第一个实体,如果没有则返回null。 public static voidtest3() \{ NavigableMap<Integer,String>navigableMap = new TreeMap<>(); navigableMap.put(5,"5"); navigableMap.put(32,"33"); navigableMap.put(13,"13"); navigableMap.put(1,"1"); Entry<Integer,String> ceilingEntry =navigableMap.ceilingEntry(5);//用来获取大于或者等于给定的key的第一个entry,如果没有的话就返回null System.out.println(ceilingEntry); Entry<Integer,String> floorEntry =navigableMap.floorEntry(4);//用来获取小于或者等于给定的key的第一个entry,如果没有的话就返回null。 System.out.println(floorEntry); Entry<Integer,String> higherEntry =navigableMap.higherEntry(5);//用来获取大于给定的key的第一个entry,如果没有的话就返回null System.out.println(higherEntry); Entry<Integer,String> lowerEntry =navigableMap.lowerEntry(4);//用来获取小于给定的key的第一个entry,如果没有的话就返回null。 System.out.println(lowerEntry); Entry<Integer,String> firsterEntry =navigableMap.firstEntry();//用于获取Map第一个entry,如果没有则返回null System.out.println(firsterEntry); Entry<Integer,String> lastEntry = navigableMap.lastEntry();//用于获取Map最后一个entry,如果没有则返回null System.out.println(lastEntry); \} **补充**:还有两个方法用来从navigableMap中返回第一、最后一个Entry,并删除,如果Map为空则返回null public static voidtest4() \{ NavigableMap<Integer,String>navigableMap = new TreeMap<>(); navigableMap.put(5,"5"); navigableMap.put(32,"33"); navigableMap.put(13,"13"); navigableMap.put(1,"1"); System.out.println("before:"+navigableMap.toString()); Entry<Integer,String> pollFirstEntry =navigableMap.pollFirstEntry();//获取Map第一个键的实体并且从Map中移除该实体,如果Map为空则返回null。 System.out.println(pollFirstEntry); System.out.println("after:"+navigableMap.toString()); Entry<Integer,String> pollLastEntry =navigableMap.pollLastEntry();//获取Map最后一个键的实体并且从Map中移除该实体,如果Map为空则返回null。 \} 3)用于获取Map Key: * ceilingKey(key):用来获取大于或者等于给定的key的第一个键,如果没有的话就返回null。 * floorKey(key):用来获取小于或者等于给定的key的第一个键,如果没有的话就返回null。 * higherKey(key):用来获取大于给定的key的第一个键,如果没有的话就返回null。 * lowerKey(key):用来获取小于给定的key的第一个键,如果没有的话就返回null。 public static voidtest2() \{ NavigableMap<Integer,String>navigableMap = new TreeMap<>(); navigableMap.put(5,"5"); navigableMap.put(32,"33"); navigableMap.put(13,"13"); navigableMap.put(1,"1"); IntegerceilingKey = navigableMap.ceilingKey(5);//用来获取大于或者等于给定的key的第一个键,如果没有的话就返回null System.out.println(ceilingKey); IntegerfloorKey = navigableMap.floorKey(4);//用来获取小于或者等于给定的key的第一个键,如果没有的话就返回null。 System.out.println(floorKey); IntegerhigherKey = navigableMap.higherKey(5);//用来获取大于给定的key的第一个键,如果没有的话就返回null System.out.println(higherKey); IntegerlowerKey = navigableMap.lowerKey(4);//用来获取小于给定的key的第一个键,如果没有的话就返回null。 System.out.println(lowerKey); \} 4)其他: 另外还有两个值得起到的NavigableMap方法:descendingKeySet()和descendingEntrySet()。keySet()和entrySet()返回的是升序的键的集合,而新的NavigableMap方法是以降序进行工作的。 参考:[http://blog.sina.com.cn/s/blog\_8a99ddf20101d2fp.html][http_blog.sina.com.cn_s_blog_8a99ddf20101d2fp.html] [http_blog.sina.com.cn_s_blog_8a99ddf20101d2fp.html]: http://blog.sina.com.cn/s/blog_8a99ddf20101d2fp.html
相关 |= 与 &= 背景:在看开源项目源码的时候,看见如下操作,|=,第一眼看到 | 很懵逼,还有这种操作!!! private boolean restrictSystemProps 待我称王封你为后i/ 2022年12月29日 00:07/ 0 赞/ 165 阅读
相关 Java集合之NavigableMap与NavigableSet接口 本文接着上篇介绍SortedMap和SortedSet接口,介绍他们的扩展接口NavigableMap与NavigableSet接口, 提供了针对给定搜索目标返回最接近匹 一时失言乱红尘/ 2022年12月14日 09:08/ 0 赞/ 192 阅读
相关 Java 集合框架系列九:JDK 1.8 SortedSet 和 NavigableSet 详解 SortedSet 继承关系 ![在这里插入图片描述][2020090818320921.png_pic_center] SortedSet 直接继承了 Set,是 我会带着你远行/ 2022年12月05日 01:37/ 0 赞/ 141 阅读
相关 UI自动化与OID与MondgoDB与MIB与面向对象。 开发基于SNMP MIB的网管多年。 现在来看MongoDB与mib的思想有几分相似。 今天我们来讨论一个UI自动化与面向对象以及MongoDB这种线性数据库的一 左手的ㄟ右手/ 2022年08月13日 13:58/ 0 赞/ 264 阅读
相关 |与||,&与&&区别 &,&&:(与,短路与):一样的地方就是二者执行最后的结果是一样的,但是执行的过程有区别, 对于&:无论&左边是否为false,他都会继续检验右边的boolean值。 对于 痛定思痛。/ 2022年06月09日 04:59/ 0 赞/ 247 阅读
相关 java &与&& |与||的区别 一、与操作和或操作的区别 (1)在Java程序中,使用与操作,要求所有表达式的判断结果都是TRUE,才为真,若有一个为FALSE,那么最终判断结果则为FALSE (2)使用 深藏阁楼爱情的钟/ 2022年04月08日 18:53/ 0 赞/ 342 阅读
相关 Java NavigableMap 接口 Java NavigableMap 接口 Java 集合框架的NavigableMap接口提供了在地图条目之间导航的功能。 它被认为是SortedMap的一种。 实现... 朱雀/ 2022年02月19日 12:00/ 1 赞/ 11027 阅读
相关 mounted 与 methods 与 computed 与 watched区别 首先讲一下vue的生命周期 beforecreate : 举个栗子:可以在这加个loading事件 created :在这结束loading,还做一些初始化,实现函数自执 刺骨的言语ヽ痛彻心扉/ 2021年12月14日 13:27/ 0 赞/ 308 阅读
相关 Java中的Map【三】NavigableMap接口 所使用的jdk版本为1.8版本,先看一下NavigableMap<K,V>在JDK中Map的UML类图中的位置: ![watermark_type_ZmFuZ3 一时失言乱红尘/ 2021年09月20日 23:14/ 0 赞/ 538 阅读
相关 NavigableMap与NavigableSet Java集合框架(JavaCollections Framework)加入了一个新的NavigableMap和NavigableSet接口——可导航的Map和集合。分别的扩展了 向右看齐/ 2021年06月24日 14:37/ 0 赞/ 426 阅读
还没有评论,来说两句吧...