Java : SortedMap 示例

Java 中的 SortedMap (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 SortedMap<K,V> 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

进一步提供其键的全排序的 Map。该 Map 根据其键的自然排序进行排序,或者通过通常在创建有序 Map 时提供的 Comparator 进行排序。在迭代有序 Map 的集合视图(由 entrySet、keySet 和 values 方法返回)时会反映此顺序。 (机器翻译)

Class diagram

final SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("ccc", 10);
sortedMap.put("ddd", 20);
sortedMap.put("aaa", 30);
sortedMap.put("bbb", 40);

System.out.println(sortedMap); // {aaa=30, bbb=40, ccc=10, ddd=20}

final var hashMap = new HashMap<String, Integer>();
hashMap.put("ccc", 10);
hashMap.put("ddd", 20);
hashMap.put("aaa", 30);
hashMap.put("bbb", 40);

System.out.println(hashMap); // {aaa=30, ccc=10, bbb=40, ddd=20}

Methods

Comparator<? super K> comparator ()

返回用于对此映射中的键进行排序的比较器,如果此映射使用其键的自然排序,则返回 null。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator); // null
final SortedMap<String, Integer> map = new TreeMap<>(Comparator.reverseOrder());
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator.equals(Comparator.reverseOrder())); // true
final SortedMap<String, Integer> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
System.out.println(map); // {}

final var comparator = map.comparator();
System.out.println(comparator.equals(String.CASE_INSENSITIVE_ORDER)); // true

Set<Map.Entry<K,V>> entrySet ()

返回此映射中包含的映射的集合视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var entries = map.entrySet();
System.out.println(entries); // [a=1, b=2, c=3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(entries); // [a=1, b=20, c=3]

entries.remove(Map.entry("a", 1));
System.out.println(map); // {b=20, c=3}
System.out.println(entries); // [b=20, c=3]

for (final var entry : entries) {
    entry.setValue(entry.getValue() * 10);
}

System.out.println(map); // {b=200, c=30}
System.out.println(entries); // [b=200, c=30]

K firstKey ()

返回此映射中当前第一个(最低)的键。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

map.put("b", 20);

System.out.println(map); // {b=20}
System.out.println(map.firstKey()); // b

map.put("c", 30);

System.out.println(map); // {b=20, c=30}
System.out.println(map.firstKey()); // b

map.put("a", 10);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.firstKey()); // a
final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

try {
    map.firstKey();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

SortedMap<K,V> headMap (K toKey)

返回此映射中其键严格小于 toKey 的部分视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);

System.out.println(map); // {x=10, y=20, z=30}

final var headMap = map.headMap("z");
System.out.println(headMap); // {x=10, y=20}

map.put("a", 40);

System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(headMap); // {a=40, x=10, y=20}

headMap.remove("x");

System.out.println(map); // {a=40, y=20, z=30}
System.out.println(headMap); // {a=40, y=20}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.headMap("a")); // {}
System.out.println(map.headMap("b")); // {a=10}
System.out.println(map.headMap("c")); // {a=10, b=20}
System.out.println(map.headMap("d")); // {a=10, b=20, c=30}

Set<K> keySet ()

返回此映射中包含的键的 Set 视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var keys = map.keySet();
System.out.println(keys); // [a, b, c]

map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]

keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]

K lastKey ()

返回此映射中当前的最后一个(最高)键。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

map.put("b", 20);

System.out.println(map); // {b=20}
System.out.println(map.lastKey()); // b

map.put("a", 10);

System.out.println(map); // {a=10, b=20}
System.out.println(map.lastKey()); // b

map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.lastKey()); // c
final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // {}

try {
    map.lastKey();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default V putFirst (K k, V v)

抛出 UnsupportedOperationException。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // []

try {
    map.putFirst("a", 10);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

default V putLast (K k, V v)

抛出 UnsupportedOperationException。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
System.out.println(map); // []

try {
    map.putLast("a", 10);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

default SortedMap<K,V> reversed ()

返回此地图的反向排序视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();

map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var reversedMap = map.reversed();
System.out.println(reversedMap); // {c=30, b=20, a=10}

System.out.println(reversedMap.reversed()); // {a=10, b=20, c=30}

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

返回此映射部分的视图,其键范围从 fromKey(包括)到 toKey(不包括)。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);

System.out.println(map); // {x=10, y=20, z=30}

final var subMap = map.subMap("a", "z");
System.out.println(subMap); // {x=10, y=20}

map.put("a", 40);

System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(subMap); // {a=40, x=10, y=20}

subMap.remove("x");

System.out.println(map); // {a=40, y=20, z=30}
System.out.println(subMap); // {a=40, y=20}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.subMap("a", "a")); // {}
System.out.println(map.subMap("a", "b")); // {a=10}
System.out.println(map.subMap("a", "c")); // {a=10, b=20}
System.out.println(map.subMap("a", "d")); // {a=10, b=20, c=30}

System.out.println(map.subMap("a", "d")); // {a=10, b=20, c=30}
System.out.println(map.subMap("b", "d")); // {b=20, c=30}
System.out.println(map.subMap("c", "d")); // {c=30}
System.out.println(map.subMap("d", "d")); // {}

SortedMap<K,V> tailMap (K fromKey)

返回此映射中其键大于或等于 fromKey 的部分视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

final var tailMap = map.tailMap("b");
System.out.println(tailMap); // {b=20, c=30}

map.put("d", 40);

System.out.println(map); // {a=10, b=20, c=30, d=40}
System.out.println(tailMap); // {b=20, c=30, d=40}

tailMap.remove("b");

System.out.println(map); // {a=10, c=30, d=40}
System.out.println(tailMap); // {c=30, d=40}
final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.tailMap("a")); // {a=10, b=20, c=30}
System.out.println(map.tailMap("b")); // {b=20, c=30}
System.out.println(map.tailMap("c")); // {c=30}
System.out.println(map.tailMap("d")); // {}

Collection<V> values ()

返回此映射中包含的值的 Collection 视图。 (机器翻译)

final SortedMap<String, Integer> map = new TreeMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var values = map.values();
System.out.println(values); // [1, 2, 3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]

values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]

try {
    values.add(4);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

Methods declared in Map

clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size

请参阅下面的链接。

Methods declared in SequencedMap

firstEntry, lastEntry, pollFirstEntry, pollLastEntry, sequencedEntrySet, sequencedKeySet, sequencedValues

请参阅下面的链接。


相关文章

To top of page