SortedMap、NavigableMap

SortedMap、NavigableMap

    • SortedMap
      • 获取比较器 comparator
      • 生成子视图 subMap、headMap、tailMap
      • 获取最小、最大的键 firstKey、lastKey
    • NavigableMap
      • 获取小于、小于等于、大于、大于等于指定键的且与指定键最接近的匹配项 lowerEntry、higherEntry、ceilingEntry、floorEntry、lowerKey、higherKey、ceilingKey、floorKey
      • 删除并返回最小键、最大键关联的项 pollFirstEntry、pollLastEntry
      • 倒序视图 descendingMap、descendingKeySet
      • 生成子视图 navigableKeySet、subMap、headMap、tailMap

视图:视图的底层由原 map 支持,所有对视图的修改会反映到原 map 上,反之依然。

SortedMap

获取比较器 comparator

Comparator<? super K> comparator();

返回用于决定 key 顺序的 Comparator,如果构造方法未指定 Comparator(即使用自然顺序,key 实现了 Comparable 接口),则返回 null。

public static void main(String[] args) {
    // 自然顺序,Integer 实现了 Comparable 接口
    SortedMap<Integer, Integer> treeMap1 = new TreeMap<>();
    System.out.println(treeMap1.comparator());

    // 指定了 Comparator
    SortedMap<Integer, Integer> treeMap2 = new TreeMap<>(Comparator.<Integer>naturalOrder().reversed());
    System.out.println(treeMap2.comparator());
}
null
java.util.Collections$ReverseComparator@2f0e140b

生成子视图 subMap、headMap、tailMap

SortedMap<K,V> subMap(K fromKey, K toKey);

返回 key 范围为 [fromKey, toKey) 的左闭右开的子视图。如果尝试插入 [fromKey, toKey) 范围外的 key,会抛异常 IllegalArgumentException

SortedMap<K,V> headMap(K toKey);

返回 key 范围为 (-∞, toKey) 的子视图(严格小于 toKey),其他与 subMap 方法相同

SortedMap<K,V> tailMap(K fromKey);

返回 key 范围为 [fromKey, +∞) 的子视图(大于等于 fromKey),其他与 subMap 方法相同

public static void main(String[] args) {
    SortedMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    // 左闭右开
    SortedMap<Integer, Integer> subMap = treeMap.subMap(3, 7);
    System.out.println("subMap: " + subMap);

    // 删除范围外的不起作用,不会抛异常
    subMap.remove(1);
    System.out.println();
    System.out.println("删除范围外的不起作用,不会抛异常");
    System.out.println("treeMap: " + treeMap);
    System.out.println("subMap: " + subMap);

    subMap.remove(3);
    System.out.println();
    System.out.println("treeMap: " + treeMap);
    System.out.println("subMap: " + subMap);

    subMap.put(4, 4);
    System.out.println();
    System.out.println("treeMap: " + treeMap);
    System.out.println("subMap: " + subMap);

    // 插入范围外的会抛异常
    subMap.put(7, 7);
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
subMap: {3=3, 5=5}

删除范围外的不起作用,不会抛异常
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
subMap: {3=3, 5=5}

treeMap: {1=1, 5=5, 7=7, 9=9}
subMap: {5=5}

treeMap: {1=1, 4=4, 5=5, 7=7, 9=9}
subMap: {4=4, 5=5}
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
	at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1516)
	at com.example.jdk8.map.T2.main(T2.java:37)

获取最小、最大的键 firstKey、lastKey

// 获取最小(第一个)的键
K firstKey()

// 获取最大(最后一个)的键
K lastKey();
public static void main(String[] args) {
    SortedMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    System.out.println("firstKey: " + treeMap.firstKey());
    System.out.println("lastKey: " + treeMap.lastKey());
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
firstKey: 1
lastKey: 9

NavigableMap

NavigableMap 继承了 SortedMap

public interface NavigableMap<K,V> extends SortedMap<K,V>

获取小于、小于等于、大于、大于等于指定键的且与指定键最接近的匹配项 lowerEntry、higherEntry、ceilingEntry、floorEntry、lowerKey、higherKey、ceilingKey、floorKey

获取符合条件且与指定键最接近的匹配项,没有则返回 null。换句话说就是获取符合条件的第一个匹配项。

// 返回 Entry
// 返回第一个小于 key 的 Entry
Map.Entry<K,V> lowerEntry(K key);
// 大于
Map.Entry<K,V> higherEntry(K key);
// 小于等于
Map.Entry<K,V> floorEntry(K key);
// 大于等于
Map.Entry<K,V> ceilingEntry(K key);

// 只返回 key
// 小于
K lowerKey(K key);
// 大于
K higherKey(K key);
// 小于等于
K floorKey(K key);
// 大于等于
K ceilingKey(K key);
public static void main(String[] args) {
    NavigableMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    System.out.println("lowerEntry(5): " + treeMap.lowerEntry(5));
    System.out.println("lowerKey(5): " + treeMap.lowerKey(5));

    System.out.println("higherEntry(5): " + treeMap.higherEntry(5));
    System.out.println("higherKey(5): " + treeMap.higherKey(5));

    System.out.println("floorEntry(5): " + treeMap.floorEntry(5));
    System.out.println("floorKey(5): " + treeMap.floorKey(5));

    System.out.println("floorEntry(4): " + treeMap.floorEntry(4));
    System.out.println("floorKey(4): " + treeMap.floorKey(4));

    System.out.println("ceilingEntry(5): " + treeMap.ceilingEntry(5));
    System.out.println("ceilingKey(5): " + treeMap.ceilingKey(5));

    System.out.println("ceilingEntry(6): " + treeMap.ceilingEntry(6));
    System.out.println("ceilingKey(6): " + treeMap.ceilingKey(6));
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
lowerEntry(5): 3=3
lowerKey(5): 3
higherEntry(5): 7=7
higherKey(5): 7
floorEntry(5): 5=5
floorKey(5): 5
floorEntry(4): 3=3
floorKey(4): 3
ceilingEntry(5): 5=5
ceilingKey(5): 5
ceilingEntry(6): 7=7
ceilingKey(6): 7

删除并返回最小键、最大键关联的项 pollFirstEntry、pollLastEntry

// 删除并返回最小键关联的项
Map.Entry<K,V> pollFirstEntry();
// 删除并返回最大键关联的项
Map.Entry<K,V> pollLastEntry();
public static void main(String[] args) {
    NavigableMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    System.out.println("pollFirstEntry(): " + treeMap.pollFirstEntry());
    System.out.println("treeMap: " + treeMap);

    System.out.println("pollLastEntry(): " + treeMap.pollLastEntry());
    System.out.println("treeMap: " + treeMap);
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
pollFirstEntry(): 1=1
treeMap: {3=3, 5=5, 7=7, 9=9}
pollLastEntry(): 9=9
treeMap: {3=3, 5=5, 7=7}

倒序视图 descendingMap、descendingKeySet

// 返回与原 map 顺序相反的 NavigableMap
NavigableMap<K,V> descendingMap();
// 返回与原 map 顺序相反的 NavigableSet
NavigableSet<K> descendingKeySet();
public static void main(String[] args) {
    NavigableMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    NavigableMap<Integer, Integer> descendingMap = treeMap.descendingMap();
    System.out.println("descendingMap: " + descendingMap);

    NavigableSet<Integer> descendingKeySet = treeMap.descendingKeySet();
    System.out.println("descendingKeySet: " + descendingKeySet);
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
descendingMap: {9=9, 7=7, 5=5, 3=3, 1=1}
descendingKeySet: [9, 7, 5, 3, 1]

生成子视图 navigableKeySet、subMap、headMap、tailMap

// 返回只包含 key 的视图
NavigableSet<K> navigableKeySet();
// 与 SortedMap 的 subMap、headMap、tailMap 相似,只是添加了 fromInclusive、toInclusive 用于控制是否包含边界
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                         K toKey,   boolean toInclusive);

NavigableMap<K,V> headMap(K toKey, boolean inclusive);

NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
public static void main(String[] args) {
    NavigableMap<Integer, Integer> treeMap = new TreeMap<>();
    treeMap.put(1, 1);
    treeMap.put(3, 3);
    treeMap.put(5, 5);
    treeMap.put(7, 7);
    treeMap.put(9, 9);
    System.out.println("treeMap: " + treeMap);

    NavigableSet<Integer> navigableKeySet = treeMap.navigableKeySet();
    System.out.println("navigableKeySet: " + navigableKeySet);

    System.out.println("subMap(3, true, 7, true): " + treeMap.subMap(3, true, 7, true));
    System.out.println("subMap(3, false, 7, false): " + treeMap.subMap(3, false, 7, false));

    System.out.println("headMap(3, true): " + treeMap.headMap(3, true));
    System.out.println("headMap(3, false): " + treeMap.headMap(3, false));

    System.out.println("tailMap(7, true): " + treeMap.tailMap(7, true));
    System.out.println("tailMap(7, false): " + treeMap.tailMap(7, false));
}
treeMap: {1=1, 3=3, 5=5, 7=7, 9=9}
navigableKeySet: [1, 3, 5, 7, 9]
subMap(3, true, 7, true): {3=3, 5=5, 7=7}
subMap(3, false, 7, false): {5=5}
headMap(3, true): {1=1, 3=3}
headMap(3, false): {1=1}
tailMap(7, true): {7=7, 9=9}
tailMap(7, false): {9=9}

你可能感兴趣的:(java,java,SortedMap,NavigableMap)