The TreeMap structure is a classic map structure with keys sorted by their natural order. For example, with the following codes:
Map<String, Integer> map = new TreeMap<>();
map.put("apple", 100);
map.put("orange", 10);
map.put("banana", 5);
If I want to print the map, like:
System.out.println(map);
Then the console will output (with natural order of the key):
{apple=100, banana=5, orange=10}
My question is: is there any Map structure available (UnknownMap) that could automatically sort the key by the value? If we have such structure, if we revise the codes to:
Map<String, Integer> map = new UnknownMap<>();
map.put("apple", 100);
map.put("orange", 10);
map.put("banana", 5);
System.out.println(map);
The console will output:
{banana=5, orange=10, apple=100}
Note: this question is not asking how to sort a map. Instead, this is to ask a specific sorted map implementation that efficiently do the sorting by value with any changes to the map, such as put, remove, etc.