If you want to find all entries starting with text (for example, "f"), you can use the subSet(from, to) method, for example:
SortedSet<String> s = new TreeSet<String>(new Comparator<String>() { public int compare( String s1, String s2 ) { return s1.compareToIgnoreCase( s2 ); } }); s.add( "Erich" ); s.add( "Erica" ); s.add( "Erin" ); s.add( "Dave" ); s.add( "Thomas" ); SortedSet<String> result = s.subSet( "e", "e" + Character.MAX_VALUE );
Since the to parameter on subSet(from, to) is exceptional, you need something that will be clearly larger. In my example, I just added Character.MAX_VALUE , but you might want to get a better upper bound. Note that this depends on your comparator, for example. how it handles differences in cases, etc.
If you want to filter with wildcards, like all texts containing text (for example, f will be translated to *f* ), you will have to iterate over and check all the records anyway. In this case, you will not get any advantage using sorted set.
Edit: updated the example to your data (also adding me :)).
source share