![]() |
VOOZH | about |
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.
Below is an example of how to create and after that how to add key-value pairs in a TreeMap.
TreeMap: {One=1, Three=3, Two=2}
TreeMap class from the java.util package.TreeMapExample.main method:TreeMap named numberMap with keys of type String and values of type Integer.numberMap.numberMap are displayed.Fruit TreeMap: {Banana=Yellow, Kiwi=Green, Strawberry=Red}
TreeMap class from the java.util package.TreeMapExample.main method:TreeMap named fruitMap with keys and values of type String.fruitMap.fruitMap are displayed.z