如何使用Java中的lambda和方法引用来实现IntPredicate接口?
IntPredicate接口是java.util.function包中定义的内置功能接口。该功能接口接受一个int值参数作为输入,并产生一个布尔值作为输出。此接口是Predicate接口的特殊化,并用作lambda表达式或方法引用的分配目标。它仅提供一种抽象方法test()。
语法
@FunctionalInterface
public interface IntPredicate {
boolean test(int value);
}Lambda表达式示例
import java.util.function.IntPredicate;
public class IntPredicateLambdaTest {
public static void main(String[] args) {
IntPredicate intPredicate = (int input) -> { // lambda 表达式
if(input == 100) {
return true;
} else
return false;
};
boolean result = intPredicate.test(100);
System.out.println(result);
}
}输出结果
true
方法引用示例
import java.util.function.IntPredicate;
public class IntPredicateMethodReferenceTest {
public static void main(String[] args) {
IntPredicate intPredicate = IntPredicateMethodReferenceTest::test; //方法引用
boolean result = intPredicate.test(100);
System.out.println(result);
}
static boolean test(int input) {
if(input == 50) {
return true;
} else
return false;
}
}输出结果
false
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短