Java NavigableMap why headMap returns different for the same methods
Regarding Java TreeMap method (or NavigableMap interface):
public SortedMap<K,V> headMap(K toKey)
public NavigableMap<K,V> headMap(K toKey, boolean inclusive)
Why do they return different types? I know SortedMap is super for NavigableMap, I just want to know why a different return is required for the included flag.
Looking at the code, it makes no sense either that the version without the included flag just calls another.
Thank.
+3
source to share
1 answer
I think the reason is historical. TreeMap
and headMap(K)
dates back to Java 1.2, while NavigableMap
and headMap(K, boolean)
was introduced in Java 1.6.
+4
source to share