술어 java.util.function의 인터페이스 패키지는 람다 식의 대상으로 사용할 수 있습니다. 이 인터페이스의 테스트 메소드는 값을 승인하고 광고는 Predicate 객체의 현재 값으로 유효성을 검사합니다. 이 메서드는 일치하는 경우 true를 반환하고 그렇지 않으면 false를 반환합니다.
asPredicate() java.util.regex.Pattern 메소드 클래스는 현재 Pattern 개체가 컴파일된 데 사용된 정규식과 문자열을 일치시킬 수 있는 Predicate 개체를 반환합니다.
예시 1
import java.util.Scanner;
import java.util.function.Predicate;
import java.util.regex.Pattern;
public class AsPredicateExample {
public static void main( String args[] ) {
//Reading string value
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string");
String input = sc.nextLine();
//Regular expression to find digits
String regex = "[t]";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
//Converting the regular expression to predicate
Predicate<String> predicate = pattern.asPredicate();
//Testing the predicate with the input string
boolean result = predicate.test(input);
if(result) {
System.out.println("Match found");
} else {
System.out.print("Match not found");
}
}
} 출력
Enter input string Tutorialspoint Number of matches: 3
예시 2
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.function.Predicate;
import java.util.regex.Pattern;
public class AsPredicateExample {
public static void main( String args[] ) {
ArrayList<String> list = new ArrayList<String>();
list.addAll(Arrays.asList("Java", "JavaFX", "Hbase", "JavaScript"));
//Regular expression to find digits
String regex = "[J]";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Converting the regular expression to predicate
Predicate<String> predicate = pattern.asPredicate();
list.forEach(n -> { if (predicate.test(n)) System.out.println("Match found "+n); });
}
} 출력
Match found Java Match found JavaFX Match found JavaScript