如何使用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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短