Java : TreeMap with Examples

TreeMap (Java SE 18 & JDK 18) API Examples.
You will find code examples on most TreeMap methods.


Summary

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Class diagram

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

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.get("a")); // 10
System.out.println(map.get("b")); // 20
System.out.println(map.get("X")); // null

map.remove("b");
System.out.println(map); // {a=10, c=30}
final var treeMap = new TreeMap<String, Integer>();
treeMap.put("ccc", 10);
treeMap.put("ddd", 20);
treeMap.put("aaa", 30);
treeMap.put("bbb", 40);

System.out.println(treeMap); // {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}

Constructors

TreeMap ()

Constructs a new, empty tree map, using the natural ordering of its keys.

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

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

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

map.put(100, "a");
map.put(999, "b");
map.put(-200, "c");

System.out.println(map); // {-200=c, 100=a, 999=b}

TreeMap (Comparator<? super K> comparator)

Constructs a new, empty tree map, ordered according to the given comparator.

final var src = Map.of("bbb", 10, "aaa", 20, "ccc", 30);

final var map1 = new TreeMap<String, Integer>();
map1.putAll(src);
System.out.println(map1); // {aaa=20, bbb=10, ccc=30}

final var map2 = new TreeMap<String, Integer>(Comparator.reverseOrder());
map2.putAll(src);
System.out.println(map2); // {ccc=30, bbb=10, aaa=20}

TreeMap (Map<? extends K,? extends V> m)

Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.

final var m = Map.of("b", 10, "a", 20, "c", 30);

final var map = new TreeMap<>(m);
System.out.println(map); // {a=20, b=10, c=30}
System.out.println(map.size()); // 3

TreeMap (SortedMap<K,? extends V> m)

Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

final var m = new TreeMap<String, Integer>(Comparator.reverseOrder());
m.put("a", 10);
m.put("b", 20);
m.put("c", 30);

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

final var map = new TreeMap<>(m);
System.out.println(map); // {c=30, b=20, a=10}
System.out.println(map.size()); // 3

Methods

Map.Entry<K,V> ceilingEntry (K key)

Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.ceilingEntry("a")); // b=10
System.out.println(map.ceilingEntry("b")); // b=10
System.out.println(map.ceilingEntry("c")); // d=20
System.out.println(map.ceilingEntry("d")); // d=20
System.out.println(map.ceilingEntry("e")); // f=30
System.out.println(map.ceilingEntry("f")); // f=30
System.out.println(map.ceilingEntry("g")); // null

K ceilingKey (K key)

Returns the least key greater than or equal to the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.ceilingKey("a")); // b
System.out.println(map.ceilingKey("b")); // b
System.out.println(map.ceilingKey("c")); // d
System.out.println(map.ceilingKey("d")); // d
System.out.println(map.ceilingKey("e")); // f
System.out.println(map.ceilingKey("f")); // f
System.out.println(map.ceilingKey("g")); // null

void clear ()

Removes all of the mappings from this map.

final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3

map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0

Object clone ()

Returns a shallow copy of this TreeMap instance.

final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}

final var cloned = map.clone();
System.out.println(cloned); // {a=1, b=2, c=3}
System.out.println(cloned.getClass()); // class java.util.TreeMap

Comparator<? super K> comparator ()

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

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

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

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

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

V compute (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

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

final var func = new BiFunction<String, Integer, Integer>() {
    @Override
    public Integer apply(String key, Integer value) {
        if (value == null) {
            return -100;
        } else {
            return value * 10;
        }
    }
};

final var ret1 = map.compute("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}

final var ret2 = map.compute("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}

final var ret3 = map.compute("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}

final var ret4 = map.compute("x", func);
System.out.println(ret4); // -100
System.out.println(map); // {a=10, b=20, c=30, x=-100}
final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var ret = map.compute("b", (key, value) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}

V computeIfAbsent (K key, Function<? super K,? extends V> mappingFunction)

If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.

final var map = new TreeMap<String, String>();
map.put("a", "aaa");
map.put("b", "bbb");
System.out.println(map); // {a=aaa, b=bbb}

final var func = new Function<String, String>() {
    @Override
    public String apply(String key) {
        if ("c".equals(key)) {
            return "ccc";
        } else {
            return null;
        }
    }
};

final var ret1 = map.computeIfAbsent("a", func);
System.out.println(ret1); // aaa
System.out.println(map); // {a=aaa, b=bbb}

final var ret2 = map.computeIfAbsent("b", func);
System.out.println(ret2); // bbb
System.out.println(map); // {a=aaa, b=bbb}

final var ret3 = map.computeIfAbsent("c", func);
System.out.println(ret3); // ccc
System.out.println(map); // {a=aaa, b=bbb, c=ccc}

final var ret4 = map.computeIfAbsent("d", func);
System.out.println(ret4); // null
System.out.println(map); // {a=aaa, b=bbb, c=ccc}
final var map = new TreeMap<String, List<String>>();
System.out.println(map); // {}

map.computeIfAbsent("a", (key) -> new ArrayList<>()).add("a-1");
System.out.println(map); // {a=[a-1]}

map.computeIfAbsent("a", (key) -> new ArrayList<>()).add("a-2");
System.out.println(map); // {a=[a-1, a-2]}

map.computeIfAbsent("b", (key) -> new ArrayList<>()).add("b-1");
System.out.println(map); // {a=[a-1, a-2], b=[b-1]}

V computeIfPresent (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

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

final var func = new BiFunction<String, Integer, Integer>() {
    @Override
    public Integer apply(String key, Integer value) {
        return value * 10;
    }
};

final var ret1 = map.computeIfPresent("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}

final var ret2 = map.computeIfPresent("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}

final var ret3 = map.computeIfPresent("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}

final var ret4 = map.computeIfPresent("X", func);
System.out.println(ret4); // null
System.out.println(map); // {a=10, b=20, c=30}
final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var ret = map.computeIfPresent("b", (k, v) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}

boolean containsKey (Object key)

Returns true if this map contains a mapping for the specified key.

final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}

System.out.println(map.containsKey("a")); // true
System.out.println(map.containsKey("b")); // true
System.out.println(map.containsKey("c")); // true
System.out.println(map.containsKey("X")); // false
System.out.println(map.containsKey("")); // false

boolean containsValue (Object value)

Returns true if this map maps one or more keys to the specified value.

final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("x", 3);

System.out.println(map); // {a=1, b=2, c=3, x=3}

System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(1)); // true
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // true
System.out.println(map.containsValue(4)); // false

NavigableSet<K> descendingKeySet ()

Returns a reverse order NavigableSet view of the keys contained in this map.

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

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

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

map.put("d", 40);

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

descKeys.remove("b");

System.out.println(map); // {a=10, c=30, d=40}
System.out.println(descKeys); // [d, c, a]

NavigableMap<K,V> descendingMap ()

Returns a reverse order view of the mappings contained in this map.

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

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

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

map.put("d", 40);

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

descMap.remove("b");

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

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

Returns a Set view of the mappings contained in this map.

final var map = new TreeMap<String, Integer>();
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]

Map.Entry<K,V> firstEntry ()

Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

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

map.put("b", 20);

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

map.put("c", 30);

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

map.put("a", 10);

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

K firstKey ()

Returns the first (lowest) key currently in this map.

final var map = new TreeMap<String, Integer>();
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 var map = new TreeMap<String, Integer>();
System.out.println(map); // {}

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

// Result
// ↓
//java.util.NoSuchElementException

Map.Entry<K,V> floorEntry (K key)

Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.floorEntry("a")); // null
System.out.println(map.floorEntry("b")); // b=10
System.out.println(map.floorEntry("c")); // b=10
System.out.println(map.floorEntry("d")); // d=20
System.out.println(map.floorEntry("e")); // d=20
System.out.println(map.floorEntry("f")); // f=30
System.out.println(map.floorEntry("g")); // f=30

K floorKey (K key)

Returns the greatest key less than or equal to the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.floorKey("a")); // null
System.out.println(map.floorKey("b")); // b
System.out.println(map.floorKey("c")); // b
System.out.println(map.floorKey("d")); // d
System.out.println(map.floorKey("e")); // d
System.out.println(map.floorKey("f")); // f
System.out.println(map.floorKey("g")); // f

V get (Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

final var map = new TreeMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}

System.out.println(map.get("a")); // 1
System.out.println(map.get("b")); // 2
System.out.println(map.get("c")); // 3
System.out.println(map.get("X")); // null
System.out.println(map.get("")); // null

SortedMap<K,V> headMap (K toKey)

Returns a view of the portion of this map whose keys are strictly less than toKey.

final var map = new TreeMap<String, Integer>();
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 var map = new TreeMap<String, Integer>();
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}

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

Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.

final var map = new TreeMap<String, Integer>();
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("y", true);
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 var map = new TreeMap<String, Integer>();
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("a", true)); // {a=10}

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

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

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

Map.Entry<K,V> higherEntry (K key)

Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.higherEntry("a")); // b=10
System.out.println(map.higherEntry("b")); // d=20
System.out.println(map.higherEntry("c")); // d=20
System.out.println(map.higherEntry("d")); // f=30
System.out.println(map.higherEntry("e")); // f=30
System.out.println(map.higherEntry("f")); // null
System.out.println(map.higherEntry("g")); // null

K higherKey (K key)

Returns the least key strictly greater than the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.higherKey("a")); // b
System.out.println(map.higherKey("b")); // d
System.out.println(map.higherKey("c")); // d
System.out.println(map.higherKey("d")); // f
System.out.println(map.higherKey("e")); // f
System.out.println(map.higherKey("f")); // null
System.out.println(map.higherKey("g")); // null

Set<K> keySet ()

Returns a Set view of the keys contained in this map.

final var map = new TreeMap<String, Integer>();
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]

Map.Entry<K,V> lastEntry ()

Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

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

map.put("b", 20);

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

map.put("a", 10);

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

map.put("c", 30);

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

K lastKey ()

Returns the last (highest) key currently in this map.

final var map = new TreeMap<String, Integer>();
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 var map = new TreeMap<String, Integer>();
System.out.println(map); // {}

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

// Result
// ↓
//java.util.NoSuchElementException

Map.Entry<K,V> lowerEntry (K key)

Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.lowerEntry("a")); // null
System.out.println(map.lowerEntry("b")); // null
System.out.println(map.lowerEntry("c")); // b=10
System.out.println(map.lowerEntry("d")); // b=10
System.out.println(map.lowerEntry("e")); // d=20
System.out.println(map.lowerEntry("f")); // d=20
System.out.println(map.lowerEntry("g")); // f=30

K lowerKey (K key)

Returns the greatest key strictly less than the given key, or null if there is no such key.

final var map = new TreeMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);

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

System.out.println(map.lowerKey("a")); // null
System.out.println(map.lowerKey("b")); // null
System.out.println(map.lowerKey("c")); // b
System.out.println(map.lowerKey("d")); // b
System.out.println(map.lowerKey("e")); // d
System.out.println(map.lowerKey("f")); // d
System.out.println(map.lowerKey("g")); // f

V merge (K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.

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

final var func = new BiFunction<Integer, Integer, Integer>() {
    @Override
    public Integer apply(Integer oldValue, Integer newValue) {
        return oldValue + newValue;
    }
};

final var ret1 = map.merge("a", 1000, func);
System.out.println(ret1); // 1001
System.out.println(map); // {a=1001, b=2, c=3}

final var ret2 = map.merge("b", 1000, func);
System.out.println(ret2); // 1002
System.out.println(map); // {a=1001, b=1002, c=3}

final var ret3 = map.merge("c", 1000, func);
System.out.println(ret3); // 1003
System.out.println(map); // {a=1001, b=1002, c=1003}

final var ret4 = map.merge("d", 1000, func);
System.out.println(ret4); // 1000
System.out.println(map); // {a=1001, b=1002, c=1003, d=1000}

final var ret5 = map.merge("b", 1000, (k, v) -> null);
System.out.println(ret5); // null
System.out.println(map); // {a=1001, c=1003, d=1000}

Map.Entry<K,V> pollFirstEntry ()

Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

final var map = new TreeMap<String, Integer>();
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.pollFirstEntry()); // a=10
System.out.println(map); // {b=20, c=30}

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

System.out.println(map.pollFirstEntry()); // c=30
System.out.println(map); // {}

System.out.println(map.pollFirstEntry()); // null

Map.Entry<K,V> pollLastEntry ()

Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

final var map = new TreeMap<String, Integer>();
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.pollLastEntry()); // c=30
System.out.println(map); // {a=10, b=20}

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

System.out.println(map.pollLastEntry()); // a=10
System.out.println(map); // {}

System.out.println(map.pollLastEntry()); // null

V put (K key, V value)

Associates the specified value with the specified key in this map.

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

System.out.println(map.put("a", 1)); // null
System.out.println(map); // {a=1}

System.out.println(map.put("b", 2)); // null
System.out.println(map); // {a=1, b=2}

System.out.println(map.put("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}

System.out.println(map.put("a", 999)); // 1
System.out.println(map); // {a=999, b=2, c=3}

void putAll (Map<? extends K,? extends V> map)

Copies all of the mappings from the specified map to this map.

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

map.putAll(Map.of());
System.out.println(map); // {}

map.putAll(Map.of("a", 1, "b", 2));
System.out.println(map); // {a=1, b=2}

map.putAll(Map.of("c", 3, "d", 4, "e", 5));
System.out.println(map); // {a=1, b=2, c=3, d=4, e=5}

map.putAll(Map.of("a", 901, "b", 902, "z", 100));
System.out.println(map);  // {a=901, b=902, c=3, d=4, e=5, z=100}

V remove (Object key)

Removes the mapping for this key from this TreeMap if present.

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

System.out.println(map.remove("b")); // 2
System.out.println(map); // {a=1, c=3}

System.out.println(map.remove("z")); // null
System.out.println(map); // {a=1, c=3}

int size ()

Returns the number of key-value mappings in this map.

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

System.out.println(map); // {}
System.out.println(map.size()); // 0

map.put("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.size()); // 1

map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.size()); // 2

map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3

NavigableMap<K,V> subMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

Returns a view of the portion of this map whose keys range from fromKey to toKey.

final var map = new TreeMap<String, Integer>();
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", true, "y", true);
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 var map = new TreeMap<String, Integer>();
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", true, "c", true)); // {a=10, b=20, c=30}
System.out.println(map.subMap("a", true, "c", false)); // {a=10, b=20}
System.out.println(map.subMap("a", false, "c", true)); // {b=20, c=30}
System.out.println(map.subMap("a", false, "c", false)); // {b=20}

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

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

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

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.

final var map = new TreeMap<String, Integer>();
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 var map = new TreeMap<String, Integer>();
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)

Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

final var map = new TreeMap<String, Integer>();
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 var map = new TreeMap<String, Integer>();
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")); // {}

NavigableMap<K,V> tailMap (K fromKey, boolean inclusive)

Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

final var map = new TreeMap<String, Integer>();
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("a", false);
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 var map = new TreeMap<String, Integer>();
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("a", false)); // {b=20, c=30}

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

System.out.println(map.tailMap("c")); // {c=30}
System.out.println(map.tailMap("c", false)); // {}

Collection<V> values ()

Returns a Collection view of the values contained in this map.

final var map = new TreeMap<String, Integer>();
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]

//values.add(4); // UnsupportedOperationException

Methods declared in AbstractMap

equals, hashCode, isEmpty, toString

Please see the link below.

Methods declared in Map

equals, forEach, getOrDefault, hashCode, isEmpty, putIfAbsent, remove, replace, replace, replaceAll

Please see the link below.


Related posts

To top of page