This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
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: 24199

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!