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方法是以降序进行工作的。