• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subset doubt

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If I have a TreeSet s, with Integer objects in it, having the following values:

s = [606, 608, 609, 610, 612]

And I run the following code, in a method:



At the end of this, s2 contains: [608, 609, 610]

Now, according to K&B, the booleans in the subset method above indicate whether the endpoint is exclusive. I am unable to understand that bit. Can someone please point out how exactly do the booleans work in the subSet/subMap syntax?

help appreciated and requested :-)

Best,
aditya

 
Ranch Hand
Posts: 76
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Aditya,

According to Java 6 API, TreeSet implements NavigableSet, which extends SortedSet which exdends Set. The method subSet is defined in SordedSet as

It returns a view of the portion of the set whose elements range from fromElement, inclusive, to toElement, exclusive.

In Java 6, you can also use the subSet method from the NavigableSet, which is defined as:

There you have also the two boolean flags fromInclusive and toInclusive. It returns a view of the portion of the set whose elements are greater than (or equal to, if fromInclusive is true) fromElement and are less than (or equal to, if toInclusive is true) toElement.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear aditya, notice the underlind bolded parts

Aditya Kumar wrote:Hi,

s = [606, 608, 609, 610, 612]

And I run the following code, in a method:

TreeSet<Integer> s2 = new TreeSet<Integer>();
s2 = (TreeSet)s.subSet(608,true,611, true);

At the end of this, s2 contains: [608, 609, 610]

 
Aditya Kumar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Rene and Imad,

Thank you for your answers, I get it now. I was actually confused -- I took the ToInclusive argument as ToExclusive, for some reason. It's clear now. thanks again!

Best,
aditya
 
reply
    Bookmark Topic Watch Topic
  • New Topic