wood burning stoves
The moose likes Beginning Java and the fly likes Collections out of range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections out of range" Watch "Collections out of range" New topic

Collections out of range

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
The following code is an example in K & B. Why are # 5 and 6 "out of range"? # 5 is included in map but why the comment "out of range". why does #6 throw error?

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
They're out of range for the submap, not the original map. The submap only has keys between "b" and "g" (inclusive of "b" but not of "g") because that's how the submap was created.

#5 is OK because even though "r" is outside submap's range from "b" to "g", "r" isn't being added to the submap, but to the main map. That's fine, as the main map has no restrictions. The submap just won't ever show the "r" entry, as it's out of the submap's range.

#6 causes an error because "p" is outside the submap's range, and the code is specifically trying to add the "p" into the submap. That's not allowed, so you get an error.
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

Hi shuba,

I agree with Mike. It's simple on line 4 you have made submaps range only from 'b' to 'g'(but actually 'f'). So you can only add keys range of 'b' to 'f'. But you have try to add keys like 'r' and 'p' they are not in the key range of submap. That's why you got that IllegalArgumentExceptions.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Mike and Udara, thanks
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Note that this behaviour is documented in the Javadoc page of SortedMap, NavigableMap and TreeMap.

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Collections out of range
jQuery in Action, 3rd edition