title: How to do fast prefix string matching in Scala
body: <p>I'm using some Java code to do fast prefix lookups, using java.util.TreeSet, could I be using scala's TreeSet instead? Or a different solution?</p>
  
  <pre><code>/** A class that uses a TreeSet to do fast prefix matching
   */
  class PrefixMatcher {
    private val _set = new java.util.TreeSet[String]
  
    def add(s: String) = _set.add(s)
  
    def findMatches(prefix: String): List[String] = {
      val matches = new ListBuffer[String]
      val tailSet = _set.tailSet(prefix)
      for ( tail &lt;- tailSet.toArray ) {
        val tailString = tail.asInstanceOf[String]
        if ( tailString.startsWith(prefix) ) 
          matches += tailString
        else
          return matches.toList    
      }
  
      matches.toList
    }
  }
  </code></pre>