stream流+函数式接口+lambda表达式+Optional

函数式接口

  1. 函数式接口只能有一个抽象方法,
  2. 函数式接口可以有默认的方法default方法和static方法,但抽象方法只能有一个
  3. 函数式接口可以用**@FunctionalInterface**注解进行标注
  4. 可以重写Object内置的方法,比如equals,hashcode等等

lambda表达式

跟js的箭头函数很像。在函数式接口的简化过程中**(参数)->{…方法体}**
匿名内部类的简写方式

方法引入

  1. 静态方法引入: 类名::方法名
  2. 对象方法引入: 类名 :: 实例方法
  3. 实例方法引入: new 对象 对象实例::方法引入
  4. 构造函数引入: 类名::new
    注意点方法引入参数和返回类型必须和函数接口的参数列表和返回类型必须保持一致。

示例

    public static void staticMethod(Integer a){
   
        System.out.println("我是静态方法:"+a);
    }
    public String getString(String s){
   
        return "hello world"+s;
    }
    @Test
    public void test5(){
   
    	//静态方法引入
        ((MessageInterface) TestStream::staticMethod).get(5);
        //实例方法引入
         StringInterface stringInterface = new TestStream() :: getString;
        System.out.println(stringInterface.get("你好"));
        //构造函数引入
          ObjInterface objInterface = User :: new;
        System.out.println(objInterface.get("张三",19));
    }



}
@FunctionalInterface
interface ObjInterface{
   
    User get(String username,int age);
}
@FunctionalInterface
interface StringInterface{
   
    String get(String str);
}
@FunctionalInterface
 interface MessageInterface{
   
    void get(int a);
}

stream流
使用stream的步骤如下:
6. 创建stream;
7. 通过一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值