让星星⭐月亮告诉你,jdk1.8 Java函数式编程示例:Lambda函数/方法引用/4种内建函数式接口(功能性-/消费型/供给型/断言型)

41 篇文章 0 订阅

package unittest;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

@FunctionalInterface
interface IFuction1

{
public abstract P uppCase();
}
@FunctionalInterface
interface IFuction2

{
public abstract P uppCase(P p);
}
@FunctionalInterface
interface IFuction3<P,R>{
public abstract R trans(P p);
}
public class TestLocal{
public static void main(String[] args) {
//利用方法引用的概念可以位一个方法定义多个名字,但是要求必须是函数式接口.

	IFuction1<String> ifunction1  = "avc" :: toUpperCase;//    public String toUpperCase() {
	String str1 = ifunction1.uppCase();//实例对象::普通方法(不带参数)
	System.out.println(str1);
	
	IFuction2<String> ifunction2  = String :: toUpperCase; //    public String toUpperCase() {
	String str2 = ifunction2.uppCase("avc");//类名称::普通方法(带参数)
	System.out.println(str2);
	
	IFuction3<Integer,String> ifunction3  = String :: valueOf;//类名称::static方法(带参数) //    public static String valueOf(int i) {
	String str3 = ifunction3.trans(123456);
	System.out.println(str3.length());
	//内建函数式接口
	//1.功能型内建函数式接口:有参数 有返回值
	Function<String,Boolean> fun = "abc123"::startsWith;
	System.out.println("1功能型内建函数式接口:" + fun.apply("abc"));
	
	//2.消费型内建函数式接口:有参数 无返回值
	Consumer<String> con = System.out::println;
	con.accept("2消费型内建函数式接口:avc");
	//3.供给型内建函数式接口:无参数 有返回值
	Supplier<String> supplier = "abc"::toUpperCase;
	System.out.println("3供给型内建函数式接口:" + supplier.get());
	//4.断言型内建函数式接口:无参数 有返回值
	Predicate<String> predicate = String::isEmpty;
	System.out.println("4断言型内建函数式接口:" + predicate.test("a"));
	
	Predicate<String> fun_predicate = "abc123"::startsWith;
	System.out.println("5功能_断言型内建函数式接口:" + fun_predicate.test("abc"));
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dylanioucn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值