Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

compator and treemap key/data

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do something like this.

 
Bill Goldsworthy
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
No more fooling around. Read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic