• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

headSet() and tailSet() problem.

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags for details]

It is given in book "SCJP by kethy and bates" on page 590 that end points are always inclusive if boolean arguments are not present.
Then, Why does '8' occur in #2 and not in #1?

Please explain the reason.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, look at the documentation. This is behaving exactly as the methods are specified.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Again, look at the documentation. This is behaving exactly as the methods are specified.

I read it again. Still not able to get it. Kindly explain me the reason yourself.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDoc for java.lang.NavigableSet says

headSet: Returns a view of the portion of this set whose elements are strictly less than toElement.
tailSet: Returns a view of the portion of this set whose elements are greater than or equal to fromElement.


As you can see, headSet() is not inclusive by default.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The JavaDoc for java.lang.NavigableSet says

headSet: Returns a view of the portion of this set whose elements are strictly less than toElement.
tailSet: Returns a view of the portion of this set whose elements are greater than or equal to fromElement.


As you can see, headSet() is not inclusive by default.


But in the book I mentioned before it is different. Or am I interpreting it wrong? Kindly rectify me.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably misinterpreting, or missing, something the book is saying. But I don't have a copy with me so I can't check that for certain.

It's quite a common pattern for the bottom of a range to be inclusive by default, and the top of a range to be exclusive by default. One reason for this could be that it makes it easy to partition something into two non-overlapping ranges.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic