File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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: 24195

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
It's not a secret anymore!