Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Repositioning an object in a TreeSet?

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

I'm assuming that when an object is placed into a TreeSet that uses a custom Comparator, it's position is determined by Comparator.compare(). What if the object property that compare() uses changes after the object is already inserted into the set?

For example I may be keeping a TreeSet of Document objects sorted by their lastAccessedDate, and the lastAccessedDate of an object changes. Is there a way of making the TreeSet aware of this, or should I just remove the Document from the TreeSet before changing the date and reinsert it afterwards?

Thank you,

Yuriy
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Order appears to be determined at insertion. I don't know of a way to "re-order" the TreeSet if elements change.

[ July 27, 2005: Message edited by: marc weber ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could get into something tricky that listens for changes (see Observer and Observable), removes the object and reinserts it. Or you could create a new TreeSet from your existing TreeSet to re-sort everything before you read from it again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic