如何使用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
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便