Filtering a list of tokens using the asPredicate() method

As noted in the preceding table, the asPredicate() method creates a predicate that can be used to match an input string. Let's look at an example code listing to understand this method better:

package example.regex; import java.util.List; import java.util.stream.*; import java.util.regex.*; public class AsPredicateExample {   public static void main(String[] args)   {     final String[] monthsArr =  {"10", "0", "05", "09", "12", "15", "00", "-1", "100"};     final Pattern validMonthPattern =  Pattern.compile("^(?:0?[1-9]|1[00-2])$");     List<String> filteredMonths = Stream.of(monthsArr)       .filter(validMonthPattern.asPredicate())       .collect(Collectors.toList());  System.out.println(filteredMonths); ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.