Hi Team,

Im reading collections and there is a topic called Backed Collections. I could not understand the output below . Please throw some light on it.

TreeMap<String,String> map = new TreeMap<String,String>();
map.put("a","ant");
map.put("d","dog");
map.put("h","horse");

SortedMap<String,String> submap;
submap = map.subMap("b","g");

System.out.println(map + " " + submap);

map.put("b", "bat");
submap.put("f","fish");

map.put("r", "raccoon");

// submap.put("p" , "pig");  // out of range

System.out.println(map + "  " + submap);

** OUTPUT **

{a=ant, d=dog, h=horse}  {d=dog}
{a=ant, b=bat, d=dog, f=fish, h=horse, r= racoon} { b=bat, d=dog, f=fish}

Please explain the output and one comment inside the code which says "out of Range".

Dani AI

Generated

Short answer: map.subMap("b","g") returns a half‑open, backed view of the TreeMap whose keys are >= "b" and < "g". That’s why, given the original keys a, d, h, the submap initially shows only d (it sorts between "b" and "g") while a and h are outside the half‑open range. See the TreeMap javadoc for the half‑open/ backed‑view wording. (docs.oracle.com)

Because the submap is a view, changes on either side are visible immediately in the other: adding "b" to the backing map makes "b" appear in the submap; calling submap.put("f","fish") inserts into the backing map as well. Conversely, adding "r" to the backing map does not show in the submap because "r" is outside the submap’s key range. This backed‑view behavior and its consequences are documented in the TreeMap/NavigableMap API. (docs.oracle.com)

The commented “out of range” line is accurate: attempts to insert a key that falls outside the submap’s bounds (for example trying to put a key >= "g" into that submap) cause the view to reject it (the implementation throws an IllegalArgumentException). The API also notes the usual caveats about comparability and nulls (ClassCastException / NullPointerException) when keys aren’t comparable or nulls aren’t permitted. (docs.oracle.com)

Practical tips: the ordering is the map’s comparator or the keys’ natural order (so “b” isn’t special except by ordering). If a different inclusivity is needed, use the NavigableMap overload with booleans, e.g. map.subMap("b", true, "g", true) to include both endpoints. Remember TreeMap and its subviews aren’t synchronized; wrap with Collections.synchronizedSortedMap(...) if you need thread safety. (docs.oracle.com)

(Clarifying note for : ’s explanation is correct — “range” means ordering range, not a fixed count of elements.)

Recommended Answers

All 4 Replies

TreeMaps are always sorted by their keys.
subMap returns a view of a contiguous range of keys of the TreeMap, including only those keys within the specified range.
Because it's a view, updates to it just update the origional (backing) TreeMap.
Similarly updates to the origional TreeMap arre immediate visible in the view (provided they are inside the range specified when the subMap as created).
Because the view is only a specified range of keys, any attempt to add a key to it must be within that range. Anything outside that is an error.

oh ok . so i have intialised as

submap = map.subMap("b","g");

So what is the range here ? is the range is like, "b" is the second alphabet so i can add ony two elements in the submap ?. is that what range here means?

The range of keys that will be included in the subMap view are from "b" to "g". That includes all possible keys that sort after "b" but before "g". You can add any keys within that range, but not ouside it. "b" being the second letter in the alphabet has no significance whatsoever.

ps: Technically speaking the range is from "b" inclusive to "g" exclusive

Thankss James, Got it .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.