aspose file tools*
The moose likes Java in General and the fly likes SortedSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SortedSet" Watch "SortedSet" New topic


jeff mutonho
Ranch Hand

Joined: Apr 30, 2003
Posts: 271
I have a reference to a Set object (which naturally is not sorted) and I want to convert it to a SortedSet , do I have to iterate over all elements in the Set and copy them to the SortedSet(using a TreeSet)?Could there be a more efficient way of doing this?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

Not really. Rather than writing the iteration yourself, you can use the TreeSet constructor that accepts a Collection as an argument; the TreeSet will then do that iteration itself (or use the addAll() method on an existing TreeSet.)

[Jess in Action][AskingGoodQuestions]
jeff mutonho
Ranch Hand

Joined: Apr 30, 2003
Posts: 271
I agree. Here's the link:
subject: SortedSet
Similar Threads
Can a set ever be ordered ?
Why doesn't Collection Interface "set" have a get method like List?
Quession obout cellection
Getting closest element from a TreeSet
how to easily sort the same TreeSet few times?