How to Create a TreeMap in Java and Add Key-Value Pairs in it? Last Updated : 08 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, a TreeMap maintains elements in a sorted order as it is an implementation of the SortedMap Interface. It stores key-value pairs in a sorted order. In this article, we will explore the creation of TreeMap in Java and learn the step-by-step process of adding key-value pairs. Program to Create and Add Key-Value Pairs to a TreeMap in JavaBelow is an example of how to create and after that how to add key-value pairs in a TreeMap. 1. Creating a TreeMap Java // Java program to create a TreeMap import java.util.TreeMap; public class TreeMapExample { public static void main(String[] args) { // Creating a TreeMap TreeMap<String, Integer> numberMap = new TreeMap<>(); // Adding key-value pairs numberMap.put("One", 1); numberMap.put("Two", 2); numberMap.put("Three", 3); // Displaying the TreeMap System.out.println("TreeMap: " + numberMap); } } OutputTreeMap: {One=1, Three=3, Two=2} Explanation of the Program:In the above program, tt imports the TreeMap class from the java.util package.It defines a class named TreeMapExample.Inside the main method:It creates a TreeMap named numberMap with keys of type String and values of type Integer.Key-value pairs are added to the numberMap.The contents of the numberMap are displayed.2. Adding Key-Value Pairs: Java // Java program to add key-value pairs in a TreeMap import java.util.TreeMap; public class TreeMapExample { public static void main(String[] args) { // Creating a TreeMap TreeMap<String, String> fruitMap = new TreeMap<>(); // Adding key-value pairs fruitMap.put("Strawberry", "Red"); fruitMap.put("Banana", "Yellow"); fruitMap.put("Kiwi", "Green"); // Displaying the TreeMap System.out.println("Fruit TreeMap: " + fruitMap); } } OutputFruit TreeMap: {Banana=Yellow, Kiwi=Green, Strawberry=Red} Explanation of the Program:In the above program, it imports the TreeMap class from the java.util package.It defines a class named TreeMapExample.Inside the main method:It creates a TreeMap named fruitMap with keys and values of type String.Key-value pairs representing fruits and their colors are added to the fruitMap.The contents of the fruitMap are displayed.z Comment More infoAdvertise with us Next Article How to Get TreeMap Key or Value using Index in Java? 21211adygf Follow Improve Article Tags : Java Java Programs java-TreeMap Java Examples Practice Tags : Java Similar Reads How to Copy Key-Value Pairs from One TreeMap to Another in Java? In Java, a TreeMap is a Map implementation that stores key-value pairs in a red-black tree structure. It allows insertions and deletions of key-value pairs due to its tree implementation. These operations take O(log n) time on average. In this article, we will be learning how to copy key-value pairs 2 min read How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read How to Create an Immutable TreeMap in Java and it's Benefits? A TreeMap is a component of the Java Collections Framework in Java. A TreeMap is immutable which means it cannot be changed once it has been created. This implies that you are unable to add, delete, or change any items after the TreeMap has been created. In this article, we will learn how to create 2 min read How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pa 2 min read How to Sort a TreeMap By Value in Java? In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. Sorting TreeMap by value in Java The elements in TreeMap are sorted on the basis of 3 min read How to Create TreeMap Objects using Comparable Interface in Java? In Java, the TreeMap class is an implementation of the SortedMap interface that stores key-value pairs in a sorted order based on the natural ordering of the keys. By default, the keys are sorted in ascending order. If you want to sort the keys based on a custom ordering criteria, you can use the Co 6 min read Like