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.
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?
public class PatternCounter
{
static private class MatcherIterator implements Iterator<String> {
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.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)");
new TreeMap<String, Long>(new BufferedReader(new InputStreamReader(System.in))
.lines().map(line -> {
Matcher matcher = pattern.matcher(line);
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(new MatcherIterator(matcher), Spliterator.ORDERED), false);
}).reduce(Stream.empty(), Stream::concat).collect(groupingBy(o -> o, counting()))
).forEach((k, v) -> {
System.out.printf("%s\t%s\n",k,v);
});
}
}