• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaring Open/Closed/Semi-Open intervals in subSet() - SortedSet  RSS feed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through this tutorial - http://download.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html#constructor
As it says ,generally subSet() will return a set that includes the fromElement & excludes the toElement.
To change this kind of semi-open interval to closed interval, for string elements it suggests adding "\0" at the end of the string, like

would return a subset inclusive of the element "pickle" too.

How is this possible? I mean, I have element "pickle" in my set, but no element like "pickle\0"..mustn't it be designed to throw a NoSuchElementNotFoundException or something like that?
I saw the source code,

In public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive), it compares both elements to check if fromElement<toElement & then returns the submap of the internal backing map excluding the toElement.

So, finally how is the closed interval made?..but "\0" works for string elements in a set!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:
In public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive), it compares both elements to check if fromElement<toElement & then returns the submap of the internal backing map excluding the toElement.

So, finally how is the closed interval made?..but "\0" works for string elements in a set!


In public NavigableSet<E> subSet(E start, E end) the start is inclusive and the end is exclusive.

In public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive) the start and end may be either inclusive or exclusive. You control this using the two boolean parameters. That's what they're there for. If you want both start and end to be inclusive, just set both booleans to true:
.

In general, SortedSet is an inferior version of NavigableSet where they accidentally omitted several useful methods. You usually want to look at the methods on NavigableSet and ignore the ones in SortedSet. Most implementations of SortedSet also implement NavigableSet, so this works fine. The only reason SortedSet still exists is because Sun didn't want to break old code by removing it. you should ignore it if possible. Stick to the NavigableSet interface instead.

The above statements also apply to SortedMap and NavigableMap in much the same way.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand NavigableSet is richer than SortedSet, but I was just curious on knowing how the "\0" did the magic of including the toElement in the subSet.
Thanks anyway Mike, for your kind reply.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see how the \0 would make any difference. It is tantamount to converting a Java™-style String to a C-style string which is null-terminated. Joel Spolsky's article may help.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that. The trick is that if you want to include all strings up to "pickle", you need an endpoint that is after "pickle" (using the sort order), but won't accidentally be the same as another element in the list that you didn't want to include. That's what "pickle\0" is. It sorts after "pickle", but before any other possible string beginning with "pickle". (Consult String's compareTo() method to see why.) And the chance of a '\0' appearing in the string for any other reason is minimal, since Java Strings don't use NULL to terminate (unlike C strings).

Hm, it occurs to me that you may not be aware of what \0 is. Basically it's a character with a numeric value of 0. In Java this character normally serves no useful purpose, as it doesn't correspond to any printable character in any language. But in C, this was how you indicated the end of a string. This character is technically called "NUL" and informally called "null". (As in "null-terminated string".) Just be aware it's not a Java null, but a character. Representing a character that isn't there. Anyway, this character NUL is represented in a String literal using \0, and it happens to be the Unicode character with the smallest numeric value, 0. That makes it the "smallest" thing we can add to a String that makes a new, different String that's still very close to the original String.

By analogy using Doubles instead of Strings, if I wanted a subSet() containing all values from 1 to 2 inclusive, i could use something like

I'm too lazy to figure out the minimum possible amount I could add to 2 and still get a different value in Java. But you get the idea. We figure that the chance of the list containing any value between 2 and 2.00000000000001 is really, really small. In the String version, it's not just small, but impossible.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you explained the \0, its meaning has become obvious. Thank you
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!