Why is that T s1 = (TreeSet) times1.headSet(1600); works fine whereas m1 =(TreeMap) times2.headMap(1600) gives a class cast exception. s1 and times1 being appropriately initialized TreeSets ; and m1 and times2 being appropriately initialized TreeMaps .
Shouldnt these two maps being corresponding functions of Set and Map collections behave similarly ?
Anyway, the interface (JavaDoc) states that the method should return a SortedSet and SortedMap respectively. There is nothing defined, that it must return a TreeSet for the SortedSet, and a TreeMap for the SortedMap -- although in the first case, it just happens to.
It depends on the invokation.
Java6 has the new version of the methods - those with boolean arguments. (see Java API)
Methods with boolean arguments return NavigableXxx and without boolean - SortedXxx, which is super interface of NavigableXxx and you need explicite downcasting.