基于拆分器的实现可能非常简单,如果您重用 JDK 提供的:Matcher
Spliterators.AbstractSpliterator
public class MatcherSpliterator extends AbstractSpliterator<String[]>
{
private final Matcher m;
public MatcherSpliterator(Matcher m) {
super(Long.MAX_VALUE, ORDERED | NONNULL | IMMUTABLE);
this.m = m;
}
@Override public boolean tryAdvance(Consumer<? super String[]> action) {
if (!m.find()) return false;
final String[] groups = new String[m.groupCount()+1];
for (int i = 0; i <= m.groupCount(); i++) groups[i] = m.group(i);
action.accept(groups);
return true;
}
}
请注意,拆分器提供所有匹配器组,而不仅仅是完整匹配。另请注意,此拆分器支持并行性,因为实现了拆分策略。AbstractSpliterator
通常,您将使用便利流工厂:
public static Stream<String[]> matcherStream(Matcher m) {
return StreamSupport.stream(new MatcherSpliterator(m), false);
}
这为您提供了简明扼要地编写各种复杂的面向正则表达式的逻辑的强大基础,例如:
private static final Pattern emailRegex = Pattern.compile("([^,]+?)@([^,]+)");
public static void main(String[] args) {
final String emails = "kid@gmail.com, stray@yahoo.com, miks@tijuana.com";
System.out.println("User has e-mail accounts on these domains: " +
matcherStream(emailRegex.matcher(emails))
.map(gs->gs[2])
.collect(joining(", ")));
}
哪些打印件
User has e-mail accounts on these domains: gmail.com, yahoo.com, tijuana.com
为了完整起见,您的代码将重写为
Set<String> set = matcherStream(mtr).map(gs->gs[0].toLowerCase()).collect(toSet());