I have original map (say, TreeMap). Then I get a subMap (in two ways - using SortedMap.subMap and NavigableMap.subMap) - now I have orig map and submap references.
Is there a problem if:
- I modify (also structurally) orig map - any problem with submap? Like with subList?
- If I got an iterator from orig map or from submap and then modified either orig map or submap (I get ConcurrentModificationException in either case, like in values(), getKeySet(), getEntrySet() collection views?)
- I modify submap (shall be no problem for orig map - it shall change, right? That is the purpose of subMap - to work with orig map thrugh submap as a view).
Concerning Question 1: Oracle clarifies it only about subList:
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
Concerning iterator: Oracle is not clear about both subMap(...) methods: Is both subMap(...) methods of TreeMap fall into "collection view methods" group right below?
The iterators returned by the iterator method of the collections returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.