This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes TreeSet vs Collection.Sort / Array.sort for Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "TreeSet vs Collection.Sort / Array.sort for Strings" Watch "TreeSet vs Collection.Sort / Array.sort for Strings" New topic
Author

TreeSet vs Collection.Sort / Array.sort for Strings

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Gurus

I am pondering weather to use TreeSet vs the Collections.sort / Array.sort for sorting Strings.


Basically I have a list of Strings, i need to perform the following operations on these Strings

1) Able to list Strings starting with a Prefix

2) Able to list Strings Lexically greater than a String

Any help would be greatly appreciated!

Thanks a bunch
SM
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You can do both with both.

1) both basically have the same approach, using iterators

2) TreeSet has a single method to retrieve a sub set, whereas List requires two methods - the first to find the start of the sub list. java.util.Arrays has similar methods for arrays


Now, if you don't want duplicates either, SortedSet (which TreeSet implements) is just a bit easier because it has one less method call


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
If your "dictionary" of words is small, it doesn't really matter whether you choose a List (and sort it once) or use a TreeSet. But if your dictionary is large, and you'll be doing many look-ups on it, you'd better use a Radix-tree.

HTH
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Thanks a lot. I am going to build this collection when ever a user requests a particular string. So its not like a one time thing ( like a Dictionary )

-
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet vs Collection.Sort / Array.sort for Strings