Page 1 of 1
About Question enthuware.ocpjp.v11.2.1397 :
Posted: Mon Oct 26, 2020 4:06 pm
by Deleted User 6318
Submaps of any NavigableMap must implement the NavigableMap interface.
I don't get the context here - what is a submap?
Is it related with
subMap method, or something else?
Re: About Question enthuware.ocpjp.v11.2.1397 :
Posted: Mon Oct 26, 2020 10:12 pm
by admin
It is related to the subMap method. The subMap method returns a view of the portion of this map. That view is also a NavigableMap i.e. it implements NavigableMap. You may read the details here:
https://docs.oracle.com/en/java/javase/ ... leMap.html
Re: About Question enthuware.ocpjp.v11.2.1397 :
Posted: Tue Oct 27, 2020 3:00 am
by Deleted User 6318
Yeah, I already did.
So sub map of a NavigableMap is a NavigableMap by definition.
Then why does it say "must implement"?
Is this for the people who implement another implementations of NavigableMap? That they should narrow a subMap method to return NavigableMap, not SortedMap?
But if so, it's still only a suggestion, right? Because they still can make it implement SortedMap like it is in interface definition.
Re: About Question enthuware.ocpjp.v11.2.1397 :
Posted: Tue Oct 27, 2020 4:15 am
by admin
Well, that statement has been taken directly from the JavaDoc API description (link given above). I am guessing that by must they really mean "strongly adviced" because the return type of one of the submap methods is SortedMap instead of NavigableMap. The doc says, "Methods subMap(K, K), headMap(K), and tailMap(K) are specified to return SortedMap to allow existing implementations of SortedMap to be compatibly retrofitted to implement NavigableMap, but extensions and implementations of this interface are encouraged to override these methods to return NavigableMap."
>So sub map of a NavigableMap is a NavigableMap by definition.
I am not sure what you mean by this statement. By what "definition"?
Re: About Question enthuware.ocpjp.v11.2.1397 :
Posted: Tue Oct 27, 2020 11:28 am
by Deleted User 6318
You wrote that the result of subMap method is NavigableMap - that's why it was obvious for me that sub map will always be navigable map.
But now I see there are two subMap methods, with different return types. And I will assume this is the recommendation, since I don't have better explanation for this "must".
Still I feel it should rather say something like "subMap methods should narrow return type", not "submaps must implement". How the returned instance can implement anything? It's rather a method/class which can implement anything... Unless by "submaps" they meant method, I don't know...