• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet vs Collection.Sort / Array.sort for Strings

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 )

-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic