jvm学习——Lambda表达式和函数式编程接口——丑九怪


在jdk1.8中提出了Lambda表达式和函数式编程接口,在看源码的过程中遇到很多,这里总结一些常见的函数编程式接口和Lambda表达式的语法以及实现

函数式编程接口

这里只介绍几种经常使用和在源码中经常出现的

Supplier接口
  • 无参数,有一个返回值。基本用法如下
Supplier<String> supplier = () -> {  // 泛型部分表示参数类型,后面的括号中没有东西,表示无参
    return "Supplier" + "你好"; // 这里相当于函数体,具体的逻辑在这里完成,很明显,这是一个返回值为String的函数
};
System.out.println(supplier.get()); // 这相当于调用上面所写好的函数,可以将上面的函数理解为一个匿名函数

在这里插入图片描述

Consumer接口
  • 这个接口中除了最基本的调用方法:accept()之外,还有一个default方法:
    • Consumer andThen(Consumer<? super T> after),这个方法用发如下
Consumer<String> consumer = (x) -> {  // 先定义第一个函数
	System.out.println("Consumer" + "你好" + x);
};
Consumer<String> strConsumer = (x) -> { // 定义第二个函数
    System.out.println("andThen" + x);
};
consumer.andThen(strConsumer).accept("梅西");、// 这里先调用andThen方法,参数为第二个函数
				// 再调用accept方法,参数为字符串“梅西”

在这里插入图片描述
上面的代码先执行了第一个函数的内容,转而执行第二个函数的内容,参数都是accept方法传入的参数

Function接口
  • 一个参数,一个返回值。接口中除了最基本的调用方法apply(T t)之外,还有两个default方法和一个static方法:
    • default Function<V, R> compose(Function<? super V, ? extends T> before)
    • default Function<T, V> andThen(Function<? super R, ? extends V> after)
    • static Function<T, T> identity()
Function<String, String> function = (x) -> { // 两个泛型,第一个是参数类型,第二个是返回值类型
System.out.println(x);
    return x;
};
function.andThen(function).apply("++++++++++++++++"); // 这个方法是先执行apply中的,在执行andThen中的
// compose方法同理,先执行compose方法中的,再执行apply中的
Function<String, String> strFunction = Function.identity(); // 这个方法相当于Function的默认实现方式
System.out.println(strFunction.apply("Function" + "你好"));

在这里插入图片描述
前两个输出不必多说,最后一个identity()方法做下说明,他的内部实现是直接将参数返回,相当于一个默认实现方式

Predicate接口
  • 一个参数,返回值为boolean,除了最基本的调用方法test()之外,还有三个default方法和一个static方法:
    • default Predicate and(Predicate<? super T> other)
    • default Predicate negate()
    • default Predicate or(Predicate<? super T> other)
    • static Predicate isEqual(Object targetRef)
Predicate<Integer> intPredicate = (x) -> {  // 这是第一个函数实现
	return x == 110;      // 相当于和110比较,返回一个boolean
};
Predicate<Integer> intPredicateTwo = (x) -> {// 这是第二个函数实现
    return x == 120;
};
System.out.println(intPredicate.test(110));     // 直接调用test,传参相当于用110和第一个函数中的110进行比较

System.out.println(intPredicate.or(intPredicateTwo).test(120));  // 这个or方法类似于或逻辑,参数为120,
					// 传递过程是:先将120传入第一个函数,返回一个boolean,再将120传入的第二个函数,得到结果后
					// 两个逻辑值进行或运算得到结果
Predicate<Integer> predicate = Predicate.isEqual(110); // 这个静态方法相当于直接设置了需要进行比较的值,在后续
				// 的调用的过程中直接用test中的参数和.isEqual中的参数进行比较
System.out.println(predicate.test(10));

自己实现简单函数式编程接口并使用

@FunctionalInterface
public interface FunctionInterFace<T> {
    boolean test(T t);
}
  • 上面是简单的实现,应用如下
FunctionInterFace<String>  stringFunctionInterFace = (x) -> {
	return x.equals("1");
};
System.out.println(stringFunctionInterFace.test("1"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值