• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Backed Collections: Sorted Map subMap

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

I have a question with regards to Sorted Map's subMap method. Do have a look at the following code, my question follows:



The output for the above is:

{a=ant, d=dog, h=horse} {d=dog}



If I were to change the line, in which I have made the comment 'Highlight' to The output from the second print statement is than

{}

I don't understand than why? I was actually still expecting to get the same output as before.

I am saying this based on my understanding that the subMap method works as follows:

Content: a d h
KeyValue: a, b, c, d, e, f, g, h, i, j, k, l, m, n

Since I had ("b" and "d") I thought I should still get {d=dog}. I hope someone can point out to me where I am going wrong. Thanks.

regards
John

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Paterson wrote:Since I had ("b" and "d") I thought I should still get {d=dog}. I hope someone can point out to me where I am going wrong.


As they say: when all else fails, read the instructions.

SortedMap.submap()'s documentation says, quite specifically:
"...Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive..."

HIH

Winston

[Edit] BTW, most Java methods that return "ranges" behave this way. cf: String.substring().
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if the toKey is inclusive, "d" is smaller than "dog". The method doesn't just look at the first character - it checks if toKey.compareTo(k) >= 0 for each key k until this check returns false. "d".compareTo("dog") returns something < 0.
reply
    Bookmark Topic Watch Topic
  • New Topic