TreeMap
Last updated
Was this helpful?
Last updated
Was this helpful?
Java TreeMap is a based implementation of Java’s Map interface.
The entries in a TreeMap are always sorted based on the natural ordering of the keys, or based on a custom that you can provide at the time of creation of the TreeMap.
The TreeMap class is part of Java’s collection framework. It implements the NavigableMap
interface, which in turn extends the SortedMap
interface. Following is the class hierarchy of TreeMap -
The interface provides functionalities to maintain the ordering of keys. And the interface provides functionalities to navigate through the map. For example, finding the entry just greater than or just less than the given key, finding the first and last entry in the TreeMap etc.
Since a TreeMap implements NavigableMap
interface, it has the functionalities of both the NavigableMap
as well as the SortedMap
.
Following are few key points to note about TreeMap in Java -
A TreeMap is always sorted based on keys. The sorting order follows the natural ordering of keys. You may also provide a custom to the TreeMap at the time of creation to let it sort the keys using the supplied Comparator.
A TreeMap cannot contain duplicate keys.
TreeMap cannot contain the null
key. However, It can have null
values.
TreeMap is not synchronized. Access to TreeMaps must be synchronized explicitly in a multi-threaded environment.
This example shows how to create a simple TreeMap and add new key-value pairs to it. The entries in the TreeMap will be sorted based on the natural ordering of keys -
This example demonstrates how to create a TreeMap with a custom comparator that orders the TreeMap entries in the descending order of keys -
The following example shows how to create a Case Insensitive Map by passing a custom CASE_INSENSITIVE_ORDER
comparator to the TreeMap. The TreeMap will ignore case while ordering the keys.
The following example demonstrates how to -
Find the size of a TreeMap.
Check if a given key exists in a TreeMap.
Retrieve the first entry in the TreeMap.
Retrieve the last entry in the TreeMap.
Retrieve the entry whose key is just lower than the given key.
Retrieve the entry whose key is just higher than the given key.
The example below shows how to -
Remove a key from a TreeMap.
Remove a key from a TreeMap only if it is associated with a given value.
Remove the first entry of the TreeMap.
Remove the last entry of the TreeMap.
1. Write a Java program to associate the specified value with the specified key in a Tree Map.
2. Write a Java program to copy a Tree Map content to another Tree Map.
3. Write a Java program to search a key in a Tree Map.
4. Write a Java program to search a value in a Tree Map.
5. Write a Java program to get all keys from the given a Tree Map.
6. Write a Java program to delete all elements from a given Tree Map.
7. Write a Java program to sort keys in Tree Map by using comparator.
8. Write a Java program to get a key-value mapping associated with the greatest key and the least key in a map.
9. Write a Java program to get the first (lowest) key and the last (highest) key currently in a map.
10. Write a Java program to get a reverse order view of the keys contained in a given map.
11. Write a Java program to get a key-value mapping associated with the greatest key less than or equal to the given key.
12. Write a Java program to get the greatest key less than or equal to the given key.
13. Write a Java program to get the portion of a map whose keys are strictly less than a given key.
14. Write a Java program to get the portion of this map whose keys are less than (or equal to, if inclusive is true) a given key.
15. Write a Java program to get the least key strictly greater than the given key. Return null if there is no such key.
16. Write a Java program to get a key-value mapping associated with the greatest key strictly less than the given key. Return null if there is no such key.
17. Write a Java program to get the greatest key strictly less than the given key. Return null if there is no such key.
18. Write a Java program to get NavigableSet view of the keys contained in a map.
19. Write a Java program to remove and get a key-value mapping associated with the least key in a map.
20. Write a Java program to remove and get a key-value mapping associated with the greatest key in this map.
21. Write a Java program to get the portion of a map whose keys range from a given key (inclusive), to another key (exclusive).
22. Write a Java program to get the portion of a map whose keys range from a given key to another key.
23. Write a Java program to get a portion of a map whose keys are greater than or equal to a given key.
24. Write a Java program to get a portion of a map whose keys are greater than to a given key.
25. Write a Java program to get a key-value mapping associated with the least key greater than or equal to the given key. Return null if there is no such key.
26. Write a Java program to get the least key greater than or equal to the given key. Returns null if there is no such key.