lambda表达式基本原理

一般来说,任何lambda表达式都可以看做声明在函数式接口中的单个抽象方法的实现

1. lambda的使用需要 “函数式接口” 的支持

函数式接口:接口中只有一个抽象方法的接口,称为函数式接口,

函数式接口(Functional Interface):

就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

函数式接口可以被隐式转换为lambda表达式。

函数式接口可以现有的函数友好地支持 lambda。

 

2. JAVA 常用函数式接口方法 java.util.function 包下

 

 

3. 接口通常用@FunctionalInterface标识(非必须)

 

可以通过 Lambda 表达式来创建该接口的对象 (若 Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)

可以使用注解 @FunctionalInterface 修饰可以检查是否是函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口

*/

 

例:这是一个Java 提供的 函数式接口,作用是返回一个<T>对象 (供给型几口)

package java.util.function;

 

/**

* Represents a supplier of results.

*

* <p>There is no requirement that a new or distinct result be returned each

* time the supplier is invoked.

*

* <p>This is a <a href="package-summary.html">functional interface</a>

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

*

* @param <T> the type of results supplied by this supplier

*

* @since 1.8

*/

@FunctionalInterface

public interface Supplier<T> {

 

/**

* Gets a result.

*

* @return a result

*/

T get();

}

调用如下

Supplier supplier=()->"返回一个object对象";

Object o = supplier.get();

System.out.println(o); //控制台输出 "返回一个object对象"

 

 

4. 自定义函数式接口

首先创建一个接口,如下

package simpleLambda;

 

/**

* 自定义函数式接口

* @param: <T> the type of results supplied by this supplier

* @author PangHao

* @date 2018/8/29 18:38

*/

@FunctionalInterface

public interface TestInterface<T> {

 

/**

* 自定义函数式接口的功能方法,返回和入参类型相同的一个值

* @param: <T> the type of results supplied by this supplier

* @return: <T> he type of results supplied by this supplier

* @auther: PangHao

* @date: 2018-08-29 18:39

*/

T testFunction(T t);

然后去声明这个接口,并调用他的功能性方法

TestInterface testInterface =(x)-> "Hellow "+x;

//控制台输出 Hellow PangHao

System.out.println(testInterface.testFunction("PangHao"));

注:由于入参和返回值类型相同,所以不需要规定参数类型,Java可以自行进行推导

 

5.将Lambda 作为方法参数传递

依旧是先创建个函数式接口

package simpleLambda;

 

/**

* @author P H 

* @date 2018/8/24 18:17

*/

@FunctionalInterface

public interface SimpleFilter <Integer>{

 

/**

* 功能性方法,传入一个Integer 返回一个 boolean

* @param t 一个Integer 值

* @return: boolean

* @auther: P H 

* @date: 2018/8/24 18:19

*/

boolean judge(Integer t);

}

创建一个能够接受Lambda的方法

//实际对list进行过滤的方法,可以传递不同的判定规则

//判断该list 中满足判定规则元素的个数

public static int filters(List<Integer> list, SimpleFilter<Integer> fil) {

//SimpleFilter 是自定义的一个函数式接口

int count = 0;

for (int no : list) {

//这里去调用SimpleFilter 的功能性方法并将值传入

if (fil.judge(no)) {

count++;

}

}

 

//也可以用stream 来简化这种操作(推荐)

long count1 = list.stream().filter((s) -> s > 5).count();

System.out.println("count1="+count1);

//输出true

System.out.println(count1==count);

return count;

}

调用这个方法,并将lambda表达式作为参数传入

//以Lambda表达式作为参数传递,简化遍历过滤

List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).collect(Collectors.toList());

 

//使用双冒号可以直接在控制台循环输出每个元素

list.stream().filter(str->str>5).forEach(System.out::println);

 

//用Stream同样可以实现,而且更简单

long count = list.stream().filter(str -> str > 5).count();

System.out.println("The Stream Count Is "+count);

 

//将Lambda 表达式作为方法参数传到方法中(上图的方法)

//传入一个Integer值 x,如果值 x>5 返回true,否则返回false

int filters = filters(list, x -> x > 5 ? true : false);

 

// 输出 “The Count Is 6”

System.out.println("The Function Count Is "+filters);

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章