Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SortedSet

 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!