Example: Sort a map by keys using TreeMap
Output
Map: {pos1=Java, pos2=Python, pos3=JS}
Map with sorted Key{pos1=Java, pos2=Python, pos3=JS}
In the above example, we have created a map named languages using HashMap. Here, the map is not sorted.
To sort the map, we created a treemap from the map. Now, the map is sorted by its keys.