functional programming - Java 8 Pattern predicate using Stream - how variable is inferred? -
i reading "java se 8 impatient" , see curious code. it's this:
final pattern pattern = pattern.compile("....."); final long count = stream.of("cristian","daniel","ortiz","cuellar") .filter(pattern.aspredicate()) .count(); i thought aspredicate method like
public boolean aspredicate(string stringtomatch){ ..... } but real implementation this
public predicate<string>aspredicate(){ return s -> matcher(s).find(); } i know use legal:
final long count = stream.of("cristian","daniel","ortiz","cuellar") .filter(a->pattern.matcher(a).find()) .count(); but question how stream passes string pattern instance? how "cristian","daniel","ortiz","cuellar" each passed method s -> matcher(s).find(). mean how strings somehow passed , become s variable of aspredicate method.
the predicate interface functional interface defines 1 abstract method boolean test(t t) t in case string type, since you're filtering on stream<string>. in other words, code equivalent to:
final long count = stream.of("cristian","daniel","ortiz","cuellar") .filter(new predicate<string>() { public boolean test(string s) { return matcher(s).find(); } }) .count();
Comments
Post a Comment