Why does TreeMap
of type Map
not define the methods tailMap
or headMap
.
Map<String, String> map = new TreeMap<>();
map.tailMap(); //cannot resolve method tailMap
With explicit cast it works:
((TreeMap<String, String>) map).tailMap("a");
With NavigableMap
everything is fine:
NavigableMap<String, String> map1 = new TreeMap<>();
map1.tailMap("a");
If I'm right that's because of the interface Map
lacking corresponding methods, in spite of the face that the object map
is concrete implementation of class TreeMap
that certainly does possess the such methods.
Just looking for more detailed explanation.
Thanks!
SortedMap
is the "highest" interface that defines the method. So as a rule of thumb, it is the one that should be used (just as a hint - the question was specifically aboutNavigableMap
, so referring to that is fine here) – Gurgitation