java8学习笔记

一、lambda表达式

例如:

Comparator<Integer> comparator=(x,y)->Integer.compare(x,y);

基础语法

  • -> 箭头运算符,将参数列表和方法体隔离开
  • 左侧: 参数列表
  • 右侧: 方法体、lambda体

口诀:

  • 写死小括号,拷贝右箭头,落地大括号
  • 左右遇一括号省
  • 左侧推断类型省

解释:

  1. 无论左边还是右边,如果参数列表、或者方法体中执行语句只有一个或者一条,可以省略掉小括号和大括号
  2. 左侧参数列表可以不写参数类型,因为JVM可以根据“类型推断”自动确认

语法格式

主要有以下语法格式

  1. 无参数无返回值
Runnable r1=new Runnable() {
   
    @Override
    public void run() {
   
        System.out.println("hello");
    }
};

Runnable r2 = () -> {
   
    System.out.println("hello");
};

如上:因为run方法没有参数,所以左侧括号为空,箭头右侧是run方法的实现体

  1. 一个参数,无返回值
Consumer<String> con=(str)->{
   
    System.out.println(str);
};
con.accept("哈哈哈,我真是太帅了");
  1. 一个参数,无返回值 (小括号可以省略不写)
Consumer<String> con=str->System.out.println(str);
con.accept("哈哈哈,我真是太帅了");
  1. 两个及以上的参数,有返回值,并且 Lambda 体中有多条语句
Comparator<Integer> comparator2 = (x, y) -> {
   
    System.out.println("hello");
    return Integer.compare(x, y);
};
  1. 两个及以上的参数,有返回值,并且 Lambda 体中只有1条语句 (大括号 与 return 都可以省略不写)
Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);

二、函数式接口

接口中只有一个抽象方法的接口 @FunctionalIterface,默认方法不算,必须只有一个抽象接口。个人觉得可能和类型推断等有关,否则无法确认Lamdba表达式实现的式哪一个方法

自定义函数式接口

//自定义一个接口,并标注FunctionalInterface注解,接口内有一个getVlaue抽象方法
@FunctionalInterface
public interface MyFun {
   
    Integer getValue(Integer num);
}


//新建测试类,并调用方法
@Test
public void test() {
   

    Integer num = operateMyFun(10, x -> x * x);
    System.out.println(num);

    MyFun myFun=x-> x+x;
    System.out.println(myFun.getValue(10));

}

private Integer operateMyFun(Integer num, MyFun myFun) {
   
    return myFun.getValue(num);
}

四大内置函数式接口

函数式接口 参数类型 返回类型 用途
Consumer消费型接口 T void 对类型为T的对象应用操作:void accept(T t)
Supplier提供型接口 T 返回类型为T的对象:T get()
Function<T, R>函数型接口 T R 对类型为T的对象应用操作,并返回结果为R类型的对象:R apply(T t)
Predicate断言型接口 T boolean 确定类型为T的对象是否满足某约束,并返回boolean值:boolean test(T t)

消费型接口

Consumer<String> con=(str)->{
   
    System.out.println(str);
};
con.accept("哈哈哈,我真是太帅了");

提供型接口

Supplier<Integer> supplier = () -> (int) (Math.random() * 100);
System.out.println(supplier.get());

函数型接口

Function<String,String> function=str->str.toUpperCase(Locale.ROOT);
System.out.println(function.apply("wo zhen shi tai hao kan le"));

断言型接口

Predicate<String> predicate = str -> {
   
    System.out.println("开始进行判断......");
    return "666".equals(str);
};
System.out.println(predicate.test("666"));

其他接口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kiEb7KnT-1663981833267)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3d238cdf28624779a95b46ec517b7dcf~tplv-k3u1fbpfcp-watermark.image?)]

三、引用

若 Lambda 表达式体中的内容已有方法实现,则我们可以使用“方法引用”

3.1 方法引用

对象::实例方法

Lambda 表达实体中调用方法的参数列表、返回类型必须和函数式接口中抽象方法保持一致
Consumer<String> con3 = s -> System.out.println(s);
Consumer<String> con4 = System.out::println;
con3.accept("我太帅了");
con4.accept("我太帅了");

//解释:调用的Println方法返回类型和Consumer中的accept方法一致为void,并且参数列表也一致

类::静态方法

和上述要求一致
//示例
Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
Comparator<Integer> comparator2 = (x, y) -> {
   
    System.out.println("hello");
    return Integer.compare(x, y);
};
Comparator<Integer> comparator3=Integer::compare;

类::实例方法

Lambda 参数列表中的第一个参数是方法的调用者,第二个参数是方法的参数时,才能使用 ClassName :: Method
//示例
BiPredicate<String,String> b1=(x,y)->x.equals(y);
BiPredicate<String,St
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值