Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeSet vs Collection.Sort / Array.sort for Strings  RSS feed

 
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
 
Sheriff
Posts: 21330
87
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
 
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 )

-
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!