• 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

how to easily sort the same TreeSet few times?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I am typing my own example of TreeSet.

Basically I have Objects of type Item which have a name of type String and weight of type int. :



Well, if I create a TreeSet and I start to add objects they are sorted by name as I specified in the compareTo method.



At this point my question is: How can I sort the treeSet using for example the weight?
My approach is as follows:
- I have created a comparator and I am trying to sort the TreeSet with it.



But I don't know how to sort the TreeSet without creating a new one.... I got the items sorted by weight using an array:



How can I sort the treeSet without having to create a new one?

Thanks
 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read in the API that TreeSet is a Red-black implementation of the NavigableSet interface. And the NavigableSet is a SortedSet extended with navigation methods.

Following with SortedSet the API says that is a Set whose elements are automatically sorted when the SortedSet is created.

Probably the answer to my previous answer could be that : Does not make sense to sort several times a TreeSet! , it is sorted using the sorting we specified in the moment of its creation. And when we add new elements it continues sorted.

Am I right?

Cheers.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have your TreeSet<Item> ts and it's sorted based on your compareTo() method; you
now want to sort it some other way? I believe you are right, you can't do it without creating
a new data structure.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the static Collections method

public static <T> void sort(List<T> list, Comparator<? super T> c)


? Shouldn't that work? Ok, in the end it also uses an array

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. (API)



cheers
Bob
 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about the static Collections method

public static <T> void sort(List<T> list, Comparator<? super T> c)


? Shouldn't that work? Ok, in the end it also uses an array

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. (API)



cheers



We can sort the treeSet using a List instead of an array. The code could be:


I am creating an ArrayList from a Collection, in this case a TreeSet.

Cheers
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic