Introduction
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.
Constructor
-
TreeMap()
// Java Program to Demonstrate TreeMap // using the Default Constructor // Importing required classes import java.util.*; import java.util.concurrent.*; // Main class // TreeMapImplementation public class GFG { // Method 1 // To show TreeMap constructor static void Example1stConstructor() { // Creating an empty TreeMap TreeMap<Integer, String> tree_map = new TreeMap<Integer, String>(); // Mapping string values to int keys // using put() method 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"); // Printing the elements of TreeMap System.out.println("TreeMap: " + tree_map); } // Method 2 // Main driver method public static void main(String[] args) { System.out.println("TreeMap using " + "TreeMap() constructor:\n"); // Calling constructor Example1stConstructor(); } }
- TreeMap(Comparator comp)
// Java program to Illustrate Updation of Elements // in TreeMap using put() Method // Importing required classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Initialization of a TreeMap // using Generics TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); // Inserting the elements in Map // using put() method tm.put(3, "Geeks"); tm.put(2, "Geeks"); tm.put(1, "Geeks"); // Print all current elements in map System.out.println(tm); // Inserting the element at specified // corresponding to specified key tm.put(2, "For"); // Printing the updated elements of Map System.out.println(tm); } }
- TreeMap(Map M)
// Java program to Illustrate Removal of Elements // in TreeMap using remove() Method // Importing required classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Initialization of a TreeMap // using Generics TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); // Inserting the elements // using put() method tm.put(3, "Geeks"); tm.put(2, "Geeks"); tm.put(1, "Geeks"); tm.put(4, "For"); // Printing all elements of Map System.out.println(tm); // Removing the element corresponding to key tm.remove(4); // Printing updated TreeMap System.out.println(tm); } }
- TreeMap(SortedMap sm)
// Java Program to Illustrate Iterating over TreeMap // using // Importing requried classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Initialization of a TreeMap // using Generics TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); // Inserting the elements // using put() method tm.put(3, "Geeks"); tm.put(2, "For"); tm.put(1, "Geeks"); // For-each loop for traversal over Map // via entrySet() Method for (Map.Entry mapElement : tm.entrySet()) { int key = (int)mapElement.getKey(); // Finding the value String value = (String)mapElement.getValue(); // Printing the key and value System.out.println(key + " : " + value); } } }