wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes headSet() and tailSet() problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "headSet() and tailSet() problem." Watch "headSet() and tailSet() problem." New topic
Author

headSet() and tailSet() problem.

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
[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

Joined: Apr 06, 2010
Posts: 4421
    
    8

Again, look at the documentation. This is behaving exactly as the methods are specified.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: May 26, 2003
Posts: 30753
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Apr 06, 2010
Posts: 4421
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: headSet() and tailSet() problem.