📜  Java.util.TreeMap类

📅  最后修改于: 2020-11-14 06:37:39             🧑  作者: Mango


介绍

java.util.TreeMap类是Map接口的基于Red-Black树的实现。以下是有关TreeMap的要点-

  • TreeMap类保证Map将按升序排列。

  • Map是根据键Class的自然排序方法排序的,或者是根据映射创建时提供的Comparator排序的,这取决于所使用的构造函数。

类声明

以下是java.util.TreeMap类的声明-

public class TreeMap
   extends AbstractMap
   implements NavigableMap, Cloneable, Serializable

参量

以下是java.util.TreeMap类的参数-

  • K-这是此映射维护的键的类型。

  • V-这是映射值的类型。

类的构造函数

Sr.No. Constructor & Description
1

TreeMap()

This constructor constructs a new, empty tree map, using the natural ordering of its keys.

2

TreeMap(Comparator comparator)

This constructor constructs a new, empty tree map, ordered according to the given comparator.

3

TreeMap(Map m)

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

4

TreeMap(SortedMap m)

This constructor constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

类方法

Sr.No. Method & Description
1 Map.Entry ceilingEntry(K key)

This method 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.

2 K ceilingKey(K key)

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

3 void clear()

This method removes all of the mappings from this map.

4 Object clone()

This method returns a shallow copy of this TreeMap instance.

5 Comparator comparator()

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

6 boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

7 boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

8 NavigableSet descendingKeySet()

This method returns a reverse order NavigableSet view of the keys contained in this map.

9 NavigableMap descendingMap()

This method returns a reverse order view of the mappings contained in this map.

10 Set> entrySet()

This method returns a Set view of the mappings contained in this map.

11 Map.Entry firstEntry()

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

12 K firstKey()

This method returns the first (lowest) key currently in this map.

13 Map.Entry floorEntry(K key)

This method 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.

14 K floorKey(K key)

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

15 V get(Object key)

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

16 SortedMap headMap(K toKey)

This method returns a view of the portion of this map whose keys are strictly less than toKey.

17 NavigableMap headMap(K toKey, boolean inclusive)

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

18 Map.Entry higherEntry(K key)

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

19 K higherKey(K key)

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

20 Set keySet()

This method returns a Set view of the keys contained in this map.

21 Map.Entry lastEntry()

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

22 K lastKey()

This method returns the last (highest) key currently in this map.

23 Map.Entry lowerEntry(K key)

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

24 K lowerKey(K key)

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

25 NavigableSet navigableKeySet()

This method returns a NavigableSet view of the keys contained in this map.

26 Map.Entry pollFirstEntry()

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

27 Map.Entry pollLastEntry()

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

28 V put(K key, V value)

This method associates the specified value with the specified key in this map.

29 void putAll(Map map)

This method copies all of the mappings from the specified map to this map.

30 V remove(Object key)

This method removes the mapping for this key from this TreeMap if present.

31 int size()

This method returns the number of key-value mappings in this map.

32 NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

This method returns a view of the portion of this map whose keys range from fromKey to toKey

33 SortedMap subMap(K fromKey, K toKey)

This method returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

34 SortedMap tailMap(K fromKey)

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

35 NavigableMap tailMap(K fromKey, boolean inclusive)

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

36 Collection values()

This method returns a Collection view of the values contained in this map.

方法继承

此类从以下类继承方法-

  • java.util.AbstractMap
  • java.util.Object
  • java.util.Map