Java Predicate接口定义详解
作者:思维的深度
Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,这篇文章主要介绍了Java Predicate接口的定义及示例代码,需要的朋友可以参考下
Java Predicate接口
Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值。
接口定义
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
} 它接收泛型T的输入,返回true或false。
Predicate接口通常用来:
1. 过滤集合中的元素。像List#removeIf()、Stream#filter()等方法接收Predicate来过滤元素。
2. 判断对象是否满足某条件,作为if语句的判断条件。
3. 验证对象的一些属性或状态是否正确。
4. 自定义复杂的判断逻辑作为方法的参数。Predicate的使用者无需了解判断逻辑的具体实现细节。
示例代码:
// Remove persons older than 18
list.removeIf(person -> person.age > 18);
// Check if "Hello" starts with "H"
if (startsWith("H")) { ... }
Predicate<String> startsWithH = s -> s.startsWith("H");
// Check if person has empty name
Predicate<Person> nameNotEmpty = person -> !person.name.isEmpty();
// Use Predicate to filter candidates
selectCandidates(candidates, candidate -> candidate.age > 30 &&
candidate.skills.contains("Java"));扩展:Java lamda表达式 Predicate<T>、BiFunction<T,T,R>、FunctionalInterface 应用实例说明
Java lamda表达式 Predicate<T>、BiFunction<T,T,R>、FunctionalInterface 应用实例说明
使用相对应的 函数式接口,可使编写程序在某些时候变得更高雅和灵活,下面对各种情况进行说明
ps:核心原理就是 将方法作为一个参数传到另一个方法,使该方法处理内容更丰富和灵活,在C#概念中称为 委托。
一、Predicate<T>
特点:只能接收一个参数,返回值固定是 boolean值
1.1 定义方法
/**
* 接收执行 predicate 表达式
* @param predicateLamda
* @param str
* @return
*/
public static boolean checkString(Predicate<String> predicateLamda,String str){
return predicateLamda.test(str);
}1.2 使用定义 Predicate<String> 方法
public static void main(String[] args) {
// predicate 表达式
boolean result1= checkString(str->str.equals("liyanbo"), "woshi liyanbo");
System.out.println("result1 = " + result1);
}二、BiFunction<T,T,R>固定 定义两个入参,并有一个返回值
2.1定义 BiFunction<T,T,R>
/**
* BiFunction 两个传入参数,一个返回参数
* @param biFunction
* @param firstName
* @param secondName
* @return
*/
public static Integer getBiFunction(BiFunction<String,String,Integer> biFunction,String firstName,String secondName){
//String handleName= biFunction.apply(firstName,secondName);// 处理完这个后还可以 有其它逻辑处理
Integer handleName= biFunction.apply(firstName,secondName);// 处理完这个后还可以 有其它逻辑处理
return handleName+1;
}2.2 使用定义BiFunction<T,T,R>
public static void main(String[] args) { //BiFunction 两个传入参数,一个返回参数
// BiFunction<String, String, String> biFunction = (firstName, lastName) -> firstName + " " + lastName;
// String fullName = biFunction.apply("Bill","John");
Integer fullName=getBiFunction((firstName, lastName) -> (firstName + " " + lastName).length(),"li","yanbo");
System.out.println(fullName);
}三、FunctionalInterface 接口中仅有一个方法,可随意定义多个参数和有无反会值
3.1 首先定义一个 仅有一个方法的接口, 接口里 可以定义default 已实现的方法
package com.multiplethread.multiplethread;
@FunctionalInterface
public interface FunctionInterfaceRealize {
public String getHandleResult(Integer l,Integer p);
}3.2 定义使用 该FunctionInterface 的方法备后续使用 类似 1.1和2.1
/**
* 调用 functionInterface 函数式接口
* @param functionInterfaceRealize
* @param arg1
* @param arg2
* @return
*/
public static String getFunctionInterFace(FunctionInterfaceRealize functionInterfaceRealize,Integer arg1,Integer arg2){
String handleResult= functionInterfaceRealize.getHandleResult(arg1,arg2);
handleResult=handleResult+"123";
return handleResult;
}3.3 使用 已定义可以将方法作为参数的方法
public static void main(String[] args) {
// 定义 functionInterFace ,并做相应的逻辑处理
String handleResult=getFunctionInterFace((x,y)->(x.toString()+y.toString()),1,2);
}到此将 方法作为参数的几种情况就说完了,如果有疑问可以评论,大家一起讨论一下。
到此这篇关于Java Predicate接口的文章就介绍到这了,更多相关java Predicate接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
