javacollectionstreemapsortedmap

Purpose of creating NavigableInterface?


Why NavigableMap interface was created with additional method definitions and extended with SortedMap, while they could have added it in SortedMap interface and as usual implemented them in TreeMap?

The internet is filled with answers that it was included to navigate across the map. Here what does navigate mean?


Solution

    1. SortedMap interface is already published (since 1.2). That means that there are maybe hundreds or thousands classes somewhere out there that rely on SortedMap. If you would add new methods to SortedMap in 1.6, without providing default method implementations, this would break all those classes that implement SortedMap. Given that so much stuff relies on Java, this would break literally everything on the planet. I hope that it is obvious that "not breaking everything on the entire planet" is a good enough reason for not adding some obscure methods willy-nilly (It's obviously an overly dramatic and tongue-in-cheek description of what would actually happen. If the maintainers of the Java standard library did that, then nobody would use the new version, and someone would probably file a bug report, that's it).
    2. The documentation states quite clearly what "to navigate" means:

    Methods lowerEntry, floorEntry, ceilingEntry, and higherEntry

    https://docs.oracle.com/javase/7/docs/api/java/util/NavigableMap.html

    It means that you can not only lookup and traverse, but also find entries which are "close" to the requested key.