Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NavigableMap why is headMap return different for same methods

Tags:

java

treemap

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 to NavigableMap, I just want to know why an inclusive flag requires a different return.

Looking at the code makes no sense either as the version without the inclusive flag simply calls the other.

Thanks.

like image 201
Neil Walker Avatar asked Sep 20 '26 07:09

Neil Walker


1 Answers

I think the reason is historic. TreeMap itself and headMap(K) date back to Java 1.2, whereas NavigableMap and headMap(K, boolean) have been introduced in Java 1.6.

like image 62
NPE Avatar answered Sep 21 '26 21:09

NPE