File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to  easily sort the same TreeSet few times? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to  easily sort the same TreeSet few times?" Watch "how to  easily sort the same TreeSet few times?" New topic
Author

how to easily sort the same TreeSet few times?

Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
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

Joined: Mar 20, 2009
Posts: 26
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.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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.


SCJP 88% | SCWCD 84%
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to easily sort the same TreeSet few times?
 
Similar Threads
Using java.util.HashSet for Custom Class
Soting objects in collection
sort-break pattern in java?
OCPJP Practice Exam question: Collections
Accessing private variable