Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

compator and treemap key/data

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a data relationship like this:

name -> [start,length]

I need to reference the values by their name but I need to sort by the 'start' value. Can a comparator sort on data or just a key? If so, how might I do this? I'm getting stack overflow exceptions when calling get() in the compare function.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would do something like this.

 
Bill Goldsworthy
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding Keith- that's very similar to what I have but I have to get the Node out of the Map like such (which gives me a stack overflow exception).

I edited your example to illustrate:

 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stack overflow is probably occurring because that method is kinda going in circles. The purpose of the Comparator is to compare two Node objects. This will be used by the TreeMap in order to sort the list of objects. After the elements are all in the map, then you can iterate through the map.
 
Bill Goldsworthy
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I get it now. I converted my class to use a list instead of a map and it works as it should. I decided this was ok because I don't think I'll have enough nodes to really cause a problem. Thanks again for your help.

 
reply
    Bookmark Topic Watch Topic
  • New Topic