title: Searching anagrams with Java 8
body: <p>I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000.</p>
  
  <p>Output example:</p>
  
  <pre><code>word anagram1 anagram2 anagram3 ...
  word2 anagram1 anagram2...
  </code></pre>
  
  <p>I have code, it works but very slow:</p>
  
  <pre><code>  private static List&lt;String&gt; listOfWords = new ArrayList&lt;String&gt;();
    private static List&lt;ArrayList&lt;String&gt;&gt; allAnagrams = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
  
    public static void main(String[] args) throws Exception {
      URL url = new URL("www.xxx.pl/textFile.txt");
      Scanner scanner = new Scanner(url.openStream());
      while (scanner.hasNext()) {
        String nextToken = scanner.next();
        listOfWords.add(nextToken);
      }
      scanner.close();
  
      while (listOfWords.isEmpty() == false) {
        ArrayList&lt;String&gt; anagramy = new ArrayList&lt;String&gt;();
        String wzor = listOfWords.remove(0);
        anagramy.add(wzor);
        char[] ch = wzor.toCharArray();
        Arrays.sort(ch);
        for (int i = 0; i &lt; listOfWords.size(); i++) {
          String slowo = listOfWords.get(i);
          char[] cha = slowo.toCharArray();
          Arrays.sort(cha);
          if (Arrays.equals(ch, cha)) {
            anagramy.add(slowo);
            listOfWords.remove(i);
            i--;
          }
        }
        allAnagrams.add(anagramy);
      }
  
      for (ArrayList&lt;String&gt; ar : allAnagrams) {
        String result = "";
        if (ar.size() &gt; 1) {
          for (int i = 1; i &lt; ar.size(); i++) {
            result = ar.get(i) + " ";
          }
          System.out.println(ar.get(0) + " " + result);
        }
      }
    }
  </code></pre>
  
  <p>I have to write it with Java 8 - streams but I don't know. It is possible to use Streams for reading from URL + searching anagrams? Could you help me with searching anagrams by Stream? Teacher told me that code should be shorter that mine with reading a whole list. Only a few lines, is that possible?</p>