File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 19656
    
  18

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 )

-
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: TreeSet vs Collection.Sort / Array.sort for Strings
 
Similar Threads
JDK 6 vs JDK 7
Collections Query
HashSet vs Collections.binarySearch vs TreeSet
SOAP Vs REST Vs JSON
Comparator vs comparable