Methods of TreeMap in Java

2024 年 11 月 9 日 星期六(已编辑)
/
4
摘要
Java's TreeMap class provides sorted key-value pair management with methods for basic operations (put, get, remove, containsKey/Value, clear, size, isEmpty), sorted map functions (first/last key/entry, pollFirst/LastEntry), navigable map operations for range queries (lower/floor/ceiling/higher entries/keys, subMap, headMap, tailMap), and view methods (keySet, values, entrySet, navigableKeySet, descendingKeySet, descendingMap).

Methods of TreeMap in Java

Java's TreeMap class provides a variety of methods for managing and accessing key-value pairs in a sorted manner. Here are some of the key methods:

Basic Operations

  • put(K key, V value): Inserts a mapping from the specified key to the specified value.
  • get(Object key): Returns the value associated with the specified key, or null if the key is not found.
  • remove(Object key): Removes the mapping for the specified key from the map.
  • containsKey(Object key): Returns true if the map contains a mapping for the specified key, false otherwise.
  • containsValue(Object value): Returns true if the map maps one or more keys to the specified value, false otherwise.
  • clear(): Removes all mappings from the map.
  • size(): Returns the number of key-value mappings in the map.
  • isEmpty(): Returns true if the map contains no key-value mappings, false otherwise.

Sorted Map Specific Methods

  • firstKey(): Returns the first (lowest) key currently in the map.
  • lastKey(): Returns the last (highest) key currently in the map.
  • firstEntry(): Returns a Map.Entry associated with the least key in the map.
  • lastEntry(): Returns a Map.Entry associated with the greatest key in the map.
  • pollFirstEntry(): Removes and returns a Map.Entry associated with the least key in the map.
  • pollLastEntry(): Removes and returns a Map.Entry associated with the greatest key in the map.

Navigable Map Specific Methods

(for range-based operations)

  • lowerEntry(K key) / lowerKey(K key): Returns the entry/key associated with the greatest key strictly less than the given key.
  • floorEntry(K key) / floorKey(K key): Returns the entry/key associated with the greatest key less than or equal to the given key.
  • ceilingEntry(K key) / ceilingKey(K key): Returns the entry/key associated with the least key greater than or equal to the given key.
  • higherEntry(K key) / higherKey(K key): Returns the entry/key associated with the least key strictly greater than the given key.
  • subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive): Returns a view of the portion of the map whose keys range from fromKey to toKey.
  • headMap(K toKey, boolean inclusive): Returns a view of the portion of the map whose keys are less than (or equal to, if inclusive is true) toKey.
  • tailMap(K fromKey, boolean inclusive): Returns a view of the portion of the map whose keys are greater than (or equal to, if inclusive is true) fromKey.

    View Methods

  • keySet(): Returns a Set view of the keys contained in the map.
  • values(): Returns a Collection view of the values contained in the map.
  • entrySet(): Returns a Set view of the mappings contained in the map.
  • navigableKeySet(): Returns a NavigableSet view of the keys contained in the map.
  • descendingKeySet(): Returns a NavigableSet view of the keys contained in the map in descending order.
  • descendingMap(): Returns a reverse order view of the mappings contained in the map.

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...