• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections out of range

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike and Udara, thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this behaviour is documented in the Javadoc page of SortedMap, NavigableMap and TreeMap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic