黑马程序员
常用的函数式接口–Predicate
package Lambda_Return;import java.util.function.Predicate;public class PredicateDemo {
public static void main(String[] args) {
String str="12345";boolean b=checkString(str,(s)->{
return s.length()>5;});System.out.println(b);}public static boolean checkString(String str, Predicate<String> pre){
return pre.test(str);}
}
Predicate默认方法and
Predicate默认方法or
Predicate默认方法negate
Predicate接口练习–集合信息筛选
package Lambda_Return;import java.util.ArrayList;
import java.util.function.Predicate;public class PredicateTest {
public static void main(String[] args) {
String[] arr={
"古力娜扎,女","迪丽热巴,女","戈尔马丹,男"};ArrayList<String> stringArrayList=checkString(arr,(s)->{
String[] split = s.split(",");return split[0].length()==4&&split[1].equals("女");});for (String s : stringArrayList) {
System.out.println(s);}}public static ArrayList<String> checkString(String[] arr, Predicate<String> pre){
ArrayList<String> strings = new ArrayList<>();for (String s : arr) {
if(pre.test(s))strings.add(s);}return strings;}}