TreeMap is a widely used data structure in the Java programming language. It is a part of the Java Collection Framework and is an implementation of the SortedMap and NavigableMap interfaces. It provides a way to store and retrieve data in sorted order, based on the keys. In this blog post, we will discuss what TreeMap is, how it works, and how to use it in Java, including examples of all the available methods.
What is TreeMap in Java?
A TreeMap is a collection that stores key-value pairs, where each key is unique and is associated with a specific value. Unlike HashMap, it stores the elements in sorted order, based on the keys. It is implemented using a red-black tree, which is a self-balancing binary search tree. This tree structure allows for fast and efficient data retrieval, insertion, and deletion.
Why use TreeMap in Java?
TreeMap is useful in situations where we need to store data in sorted order, based on the keys. It is particularly useful in situations where we need to perform operations such as searching, traversing, and range searching, based on the keys.
TreeMap is also useful in situations where we need to maintain the natural ordering of elements, or need to sort elements based on custom comparators. It is a good alternative for HashMap when we need to maintain the order of elements.
How to use TreeMap in Java?
Using a TreeMap in Java is relatively simple. To create a TreeMap, we first need to import the java.util
package, which contains the TreeMap class.
Create and add data in TreeMap
The example of creating a TreeMap and adding data to it is given below:
import java.util.TreeMap;
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "one");
map.put(3, "three");
map.put(2, "two");
The output of the above example code is:
{1=one, 2=two, 3=three}
Once we have created the TreeMap, we can add data to it using the put() method. The put() method takes two arguments: the first parameter is the key and the second one is the value. In the example above, we are adding three key-value pairs to the TreeMap. As we can see in the output above, the elements are stored in a sorted order based on the keys.
Get data from TreeMap
We can also retrieve data from a TreeMap using the get()
method. For example, to retrieve the value associated with the key 2, we can use the following code:
String value = map.get(2);
The output of this code will be:
two
This is because the get method accepts the key as a parameter. Hence, the key is 2 in the map
object and it has assigned a value two
.
There are many methods available in TreeMap. Some of them are given below:
Method | Description |
K firstKey() | Returns the first (lowest) key currently in this map. |
K lastKey() | Returns the last (highest) key currently in this map. |
K ceilingKey(K key) | Returns the least key greater than or equal to the given key, or null if there is no such key. |
K floorKey(K key) | Returns the greatest key less than or equal to the given key, or null if there is no such key. |
K higherKey(K key) | Returns the least key strictly greater than the given key, or null if there is no such key. |
K lowerKey(K key) | Returns the greatest key strictly less than the given key, or null if there is no such key. |
In addition to these methods, TreeMap also provides other useful methods such as remove(), clear(), and size() that work similarly to HashMap.
To know more detail about TreeMap you can visit to the official documentation by clicking here.