Introduction
The Java TreeMap class is the Red-Black tree based implementation of the Map interface.Following are the important points about TreeMap −
-
The TreeMap class guarantees that the Map will be in ascending key order.
-
The Map is sorted according to the natural sort method for the key Class, or by the Comparator provided at map creation time, that will depend on which constructor used.
Class declaration
Following is the declaration for java.util.TreeMap class −
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
Parameters
Following is the parameter for java.util.TreeMap class −
-
K − This is the type of keys maintained by this map.
-
V − This is the type of mapped values.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
TreeMap() This constructor constructs a new, empty tree map, using the natural ordering of its keys. |
2 |
TreeMap(Comparator<? super K> comparator) This constructor constructs a new, empty tree map, ordered according to the given comparator. |
3 |
TreeMap(Map<? extends K,? extends V> 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<K,? extends V> m) This constructor constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
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 |
This method returns the least key greater than or equal to the given key, or null if there is no such key. |
3 |
This method removes all of the mappings from this map. |
4 |
This method returns a shallow copy of this TreeMap instance. |
5 |
This method returns true if this map contains a mapping for the specified key. |
6 |
This method returns true if this map maps one or more keys to the specified value. |
7 |
This method returns a reverse order NavigableSet view of the keys contained in this map. |
8 |
This method returns a reverse order view of the mappings contained in this map. |
9 |
This method returns a Set view of the mappings contained in this map. |
10 |
This method returns a key-value mapping associated with the least key in this map, or null if the map is empty. |
11 |
This method returns the first (lowest) key currently in this map. |
12 |
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. |
13 |
This method returns the greatest key less than or equal to the given key, or null if there is no such key. |
14 |
This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
15 |
This method returns a view of the portion of this map whose keys are strictly less than toKey. |
16 |
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. |
17 |
This method returns the least key strictly greater than the given key, or null if there is no such key. |
18 |
This method returns a Set view of the keys contained in this map. |
19 |
This method returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. |
20 |
This method returns the last (highest) key currently in this map. |
21 |
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. |
22 |
This method returns the greatest key strictly less than the given key, or null if there is no such key. |
23 |
This method returns a NavigableSet view of the keys contained in this map. |
24 |
This method removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty. |
25 |
This method removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. |
26 |
This method associates the specified value with the specified key in this map. |
27 |
This method copies all of the mappings from the specified map to this map. |
28 |
This method removes the mapping for this key from this TreeMap if present. |
29 |
This method returns the number of key-value mappings in this map. |
30 |
This method returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive |
31 |
This method returns a view of the portion of this map whose keys are greater than or equal to fromKey. |
32 |
This method returns a Collection view of the values contained in this map. |
Methods inherited
This class inherits methods from the following classes −
- java.util.AbstractMap
- java.util.Object
- java.util.Map
Adding and Getting a Value from a TreeMap Example
The following example shows the usage of Java TreeMap get() method to get the value associated with the given key in the map. We”ve created a TreeMap object of Integer,Integer. Then few entries are added, and using get() we”re printing a value for a given key.
package com.tutorialspoint; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) { // creating tree map TreeMap<Integer, Integer> treemap = new TreeMap<>(); // populating tree map treemap.put(2, 2); treemap.put(1, 1); treemap.put(3, 3); treemap.put(6, 6); treemap.put(5, 5); System.out.println("Checking value for key 3"); System.out.println("Value is: "+ treemap.get(3)); } }
Let us compile and run the above program, this will produce the following result.
Checking value for key 3 Value is: three