title: How do I create a Stream of regex matches?
body: <p>I am trying to parse standard input and extract every string that matches with a specific pattern, count the number of occurrences of each match, and print the results alphabetically.  This problem seems like a good match for the Streams API, but I can't find a concise way to create a stream of matches from a Matcher.</p>
  
  <p>I worked around this problem by implementing an iterator over the matches and wrapping it into a Stream, but the result is not very readable.  How can I create a stream of regex matches without introducing additional classes?</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class PatternCounter
  {
      static private class MatcherIterator implements Iterator&lt;String&gt; {
          private final Matcher matcher;
          public MatcherIterator(Matcher matcher) {
              this.matcher = matcher;
          }
          public boolean hasNext() {
              return matcher.find();
          }
          public String next() {
              return matcher.group(0);
          }
      }
  
      static public void main(String[] args) throws Throwable {
          Pattern pattern = Pattern.compile("[a-zA-Z0-9.!#$%&amp;’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)");
  
          new TreeMap&lt;String, Long&gt;(new BufferedReader(new InputStreamReader(System.in))
              .lines().map(line -&gt; {
                  Matcher matcher = pattern.matcher(line);
                  return StreamSupport.stream(
                          Spliterators.spliteratorUnknownSize(new MatcherIterator(matcher), Spliterator.ORDERED), false);
              }).reduce(Stream.empty(), Stream::concat).collect(groupingBy(o -&gt; o, counting()))
          ).forEach((k, v) -&gt; {
              System.out.printf("%s\t%s\n",k,v);
          });
      }
  }
  </code></pre>