The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This proves to be an efficient way of sorting and storing the key-value pairs. The storing order maintained by the treemap must be consistent with equals just like any other sorted map, irrespective of the explicit comparators. The treemap implementation is not synchronized in the sense that if a map is accessed by multiple threads, concurrently and at least one of the threads modifies the map structurally, it must be synchronized externally.
Example: The following implementation demonstrates how to create, insert, and traverse through the TreeMap.
// Java code to show creation, insertion,
// searching, and traversal in a TreeMap
import java.util.*;
import java.util.concurrent.*;
public class TreeMapImplementation {
// Declaring a TreeMap
static TreeMap<Integer, String> tree_map;
// Function to create TreeMap
static void create()
{
// Creating an empty TreeMap
tree_map
= new TreeMap<Integer, String>();
System.out.println(
"TreeMap successfully"
+ " created" );
}
// Function to Insert values in
// the TreeMap
static void insert()
{
// Mapping string values to int keys
tree_map.put( 10 , "Geeks" );
tree_map.put( 15 , "4" );
tree_map.put( 20 , "Geeks" );
tree_map.put( 25 , "Welcomes" );
tree_map.put( 30 , "You" );
System.out.println(
"\nElements successfully"
+ " inserted in the TreeMap" );
}
// Function to search a key in TreeMap
static void search( int key)
{
// Checking for the key
System.out.println(
"\nIs key \""
+ key + "\" present? "
+ tree_map.containsKey(key));
}
// Function to search a value in TreeMap
static void search(String value)
{
// Checking for the value
System.out.println(
"\nIs value \""
+ value + "\" present? "
+ tree_map.containsValue(value));
}
// Function to display the elements in TreeMap
static void display()
{
// Displaying the TreeMap
System.out.println(
"\nDisplaying the TreeMap:" );
System.out.println(
"TreeMap: " + tree_map);
}
// Function to traverse TreeMap
static void traverse()
{
System.out.println( "\nTraversing the TreeMap:" );
for (Map.Entry<Integer, String> e : tree_map.entrySet())
System.out.println(e.getKey()
+ " "
+ e.getValue());
}
// Driver code
public static void main(String[] args)
{
// Creating the TreeMap
create();
// Inserting values in the TreeMap
insert();
// Search key "50" in the TreeMap
search( 50 );
// Search value "Geeks" in the TreeMap
search( "Geeks" );
// Display the elements in TreeMap
display();
// Traverse the TreeMap
traverse();
}
}
Output:
TreeMap successfully created Elements successfully inserted in the TreeMap Is key “50” present? false Is value “Geeks” present? true Displaying the TreeMap: TreeMap: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You} Traversing the TreeMap: 10 Geeks 15 4 20 Geeks 25 Welcomes 30 You