258. Java 集合 - 深入探究 NavigableMap:新增方法助力高效数据处理
NavigableMap 是 SortedMap 的扩展,它增加了一些方法,使得在有序 Map 中操作更加灵活。通过这些方法,你可以更方便地访问特定的键或条目,利用队列特性操作数据,或者在反向顺序下遍历 Map。
一、访问特定键或条目
NavigableMap 提供了一些方法,用于获取特定的键或条目:
firstKey()和firstEntry():返回Map中的最小键或最小键值对。lastKey()和lastEntry():返回Map中的最大键或最大键值对。ceilingKey(key)和ceilingEntry(key):返回大于或等于指定键的最小键或键值对。higherKey(key)和higherEntry(key):返回大于指定键的最小键或键值对(严格大于)。floorKey(key)和floorEntry(key):返回小于或等于指定键的最大键或键值对。lowerKey(key)和lowerEntry(key):返回小于指定键的最大键或键值对(严格小于)。
这些方法使你可以精确控制对 Map 中元素的访问。
示例:
java
NavigableMap<Integer, String> map = new TreeMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
System.out.println("First Key: " + map.firstKey()); // 输出 1
System.out.println("Last Key: " + map.lastKey()); // 输出 5
System.out.println("Ceiling Key of 3: " + map.ceilingKey(3)); // 输出 3
System.out.println("Higher Key of 3: " + map.higherKey(3)); // 输出 4
System.out.println("Floor Key of 3: " + map.floorKey(3)); // 输出 3
System.out.println("Lower Key of 3: " + map.lowerKey(3)); // 输出 2
二、队列式特性:从 Map 中取出元素
NavigableMap 提供了一些方法,使得 Map 具有类似队列的操作功能:
pollFirstEntry():返回并移除Map中最小的键值对。pollLastEntry():返回并移除Map中最大的键值对。
这些方法使得我们可以模拟从 Map 的两端删除元素。
示例:
java
NavigableMap<Integer, String> map = new TreeMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
System.out.println("Poll First Entry: " + map.pollFirstEntry()); // 输出 1=one
System.out.println("Poll Last Entry: " + map.pollLastEntry()); // 输出 4=four
map.forEach((key, value) -> System.out.println(key + " :: " + value));
三、反向遍历 Map
NavigableMap 还提供了一些方法,用于反向遍历 Map。你可以通过这些方法,按照降序遍历键值对。
navigableKeySet():返回一个NavigableSet,你可以通过它按顺序访问Map中的所有键。descendingKeySet():返回一个NavigableSet,你可以通过它反向访问Map中的键。descendingMap():返回一个新的NavigableMap,它是原始Map的反向视图。
这些方法使得我们能够轻松实现对 Map 键的反向遍历。
示例:
java
NavigableMap<Integer, String> map = new TreeMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
// 正序遍历
map.keySet().forEach(key -> System.out.print(key + " ")); // 输出 1 2 3 4 5
System.out.println();
// 反向遍历
NavigableSet<Integer> descendingKeys = map.descendingKeySet();
descendingKeys.forEach(key -> System.out.print(key + " ")); // 输出 5 4 3 2 1
四、获取子集视图
NavigableMap 还提供了几个方法,用于获取 Map 的子集视图。这些视图是原始 Map 的视图,任何对这些视图的更改都会反映到原始 Map 中。
subMap(fromKey, fromInclusive, toKey, toInclusive):返回一个子集视图,可以指定是否包含边界键。headMap(toKey, inclusive):返回一个子集视图,其中包含小于toKey的键,并可以指定是否包含toKey。tailMap(fromKey, inclusive):返回一个子集视图,其中包含大于或等于fromKey的键,并可以指定是否包含fromKey。
这些方法允许你更精细地控制对 Map 中部分元素的访问。
示例:
java
NavigableMap<Integer, String> map = new TreeMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
// 获取从 2 到 4 的子集视图(包括边界)
NavigableMap<Integer, String> subMap = map.subMap(2, true, 4, true);
subMap.forEach((key, value) -> System.out.println(key + " :: " + value));
运行结果:
java
2 :: two
3 :: three
4 :: four
总结
NavigableMap提供了更强大和灵活的操作,能够让我们通过一系列方法(如firstKey()、ceilingKey()、pollFirstEntry())更加精确地访问、修改和遍历Map。- 反向遍历是
NavigableMap的一个重要特性,方法如descendingKeySet()和descendingMap()使得反向操作变得非常简便。 - 子集视图方法(如
subMap()、headMap()、tailMap())为我们提供了更多灵活的数据处理方式,允许我们根据特定范围来获取部分数据视图。