title: Filtering out strings from a HashSet (or other collection)
body: <p>I am reading the contents of a txt file into a <code>HashSet</code>. The file contains almost every word in the English language, and each word becomes a String in the <code>HashSet</code>.</p>
  
  <p>In my app, characters are being added to a String. I want to check whether this String is, or can become equal to, any of the Strings in the <code>HashSet</code>. That is, say the <code>HashSet</code> contains only the String <em>apple</em>. I have a String <em>appl</em>, and now I want to filter out the <code>HashSet</code> until it becomes a set with only Strings that start with <em>appl</em> (in this case a set with only <em>apple</em>).</p>
  
  <p>I can iterate over the entire <code>HashSet</code> and use the <code>startsWith(String)</code> method, as I build a new filtered <code>HashSet</code>. But my initial <code>HashSet</code> is very big, so my question is: Is there a more efficient way to do this (perhaps using a different type of Collection?)</p>
  
  <p>Some code of how I would do it right now:</p>
  
  <pre><code>private HashSet&lt;String&gt; filter(String partOfWord){
      HashSet&lt;String&gt; filteredSet = new HashSet&lt;&gt;();
  
      for (String word : dictionary) { // dictionary is the full HashSet
          if (word.startsWith(partOfWord)) {
              filteredSet.add(word);
          }
      }
      return filteredSet;
  }
  </code></pre>