java 接口 函数_java/函数式接口-function?

1 lambda依赖上下文

1.1 测试lambda表达式

public classTest3 {public static voidmain(String[] args) {

TheInterface1 t1= () ->{};

TheInterface2 t2= () ->{};

System.out.println(t1.getClass().getInterfaces()[0]);

System.out.println(t2.getClass().getInterfaces()[0]);

}

}

@FunctionalInterfaceinterfaceTheInterface1{voidmyMethod1();

}interfaceTheInterface2{voidmyMethod2();

}

lambda表达式依赖于上下文。是个对象。是函数式接口的实现类

1.2 结合Thread理解

开启一个线程

d8a74891deac71b2671a69a927ce4b10.png

81139a0a04efab16e01d3f938c7fce09.png

new Thread(newRunnable() {

@Overridepublic voidrun() {

}

}).start();new Thread(() -> System.out.println("hello world")).start();

1.3 理解<>,菱形语法

089e59bf2eae1a7f890e355c2871e72d.png

Explicit(显示) type argument String can be replaced with <>

1.4 理解Stream

84ba1719ed9291756adf15cee766abaa.png

/*** Returns a sequential(串形) {@codeStream} with this collection as its source.

*

*

This method should be overridden when the {@link#spliterator()}

* method cannot return a spliterator that is {@codeIMMUTABLE(并发的)},

* {@codeCONCURRENT(不可变的)}, or late-binding(延迟绑定的). (See {@link#spliterator()}

* for details.)

*

* @implSpec

* The default implementation creates a sequential {@codeStream} from the

* collection's {@codeSpliterator}.

*

*@returna sequential {@codeStream} over the elements in this collection

*@since1.8*/

同上:调用stream后,返回一个Stream对象

c0c18383568466e0d7a032095ee6382e.png

/*** Returns a stream consisting(组成) of the results of applying the given

* function to the elements of this stream.

*

*

This is an intermediate

* operation(中间操作).

*

*@param The element type of the new stream

*@parammapper a non-interfering(无干扰),

* stateless(无状态)

* function to apply to each element

*@returnthe new stream*/

2a17af994d015e35522d2c804739700f.png

/*** Represents(代表) a function that accepts one argument and produces a result.

*

*

This is a functional interface

* whose functional method is {@link#apply(Object)}.

*

*@param the type of the input to the function

*@param the type of the result of the function

*

*@since1.8*/

1009a9c6a59f584d8688d64221d12117.png

d09ae0454152b30f4970b90c7d7bb77d.png

/*** Applies(应用) this function to the given argument.

*

*@paramt the function argument

*@returnthe function result*/

可以看到map的参数是Function,是个函数式接口。本就可以使用lambda/方法引用实现。可以点击上述标注的地方,确认具体实现的地方。。。

d82208c5c4c03e67f875e3aa60c95edf.png

使用?String::toUpperCase是方法引用,类名直接引用共有方法,类名.共有方法不可行,因为方法不是静态方法,但是方法引用就可以

1.5 理解语句和表达式

8dfb5a64ae010e4cf6b575166bfc682f.png

2 lambda,传递的是一个行为

2.1 传递行为

552bb107a75ee6ab1a9c317cb395eae1.png

2.2 两个function的串联

4298b7bd871f066e5ee1ccea8b6b764f.png

8df039cbbadaf67f5dfdee7e538dbf3e.png

/*** Returns a composed(组合) function that first applies the {@codebefore}

* function to its input, and then applies this function to the result.

* If evaluation(计算) of either function throws an exception, it is relayed to

* the caller of the composed function.

*

*@param the type of input to the {@codebefore} function, and to the

* composed function

*@parambefore the function to apply before this function is applied

*@returna composed function that first applies the {@codebefore}

* function and then applies this function

*@throwsNullPointerException if before is null

*

*@see#andThen(Function)*/

03e9a9c85293a3658be1d4de2694e96b.png

/*** Returns a composed function that first applies this function to

* its input, and then applies the {@codeafter} function to the result.

* If evaluation of either function throws an exception, it is relayed to

* the caller of the composed function.

*

*@param the type of output of the {@codeafter} function, and of the

* composed function

*@paramafter the function to apply after this function is applied

*@returna composed function that first applies this function and then

* applies the {@codeafter} function

*@throwsNullPointerException if after is null

*

*@see#compose(Function)*/

2.3 Bifunction,输入两个参数,返回一个值

5d787f698d680628fc3bd975cca7f48d.png

98b916737c65ff04e16830b5f3ee60de.png

/*** Represents a function that accepts two arguments and produces a result.

* This is the two-arity specialization(两个参数的特例) of {@linkFunction}.

*

*

This is a functional interface

* whose functional method is {@link#apply(Object, Object)}.

*

*@param the type of the first argument to the function

*@param the type of the second argument to the function

*@param the type of the result of the function

*

*@seeFunction

*@since1.8*/

ac2517ddc6775997f402088e8187cbc2.png

/*** Returns a composed function that first applies this function to

* its input, and then applies the {@codeafter} function to the result.

* If evaluation of either function throws an exception, it is relayed to

* the caller of the composed function.

*

*@param the type of output of the {@codeafter} function, and of the

* composed function

*@paramafter the function to apply after this function is applied

*@returna composed function that first applies this function and then

* applies the {@codeafter} function

*@throwsNullPointerException if after is null*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值