Lambda

Lambda表达式

  1. What
    是一种简洁的可传递匿名函数:它没有名称,但它有参数列表、函数主体、返回类型,可能还有一个可以抛出的异常列表。简单来说就是匿名函数。
  2. Why
    简明的传递代码,从而使代码更清晰更灵活(并不能做Java8之前不能做的事情,单纯简化代码)。
  3. How

1.怎么用
Lambda的基本语法

(parameters) -> expression 

或者

(parameters) -> { statements; } 

参数说明

参数名称参数类型参数作用
parameters参数列表提供Lambda主体中所需要的参数
->箭头把参数列表与 Lambda 主体分隔开
expression / statementsLambda 主体具体的返回值或者代码逻辑实现。主体为表达式时不用花括号包裹,主体为语句时需要将所有语句用花括号({})包裹。注:方法调用的返回值为空时,不需要使用括号环绕返回值为空的单行方法调用。示例:process(() -> System.out.println(“This is awesome”));

有效的Lambda例子:

(String s) -> s.length() 
//返回参数s的长度。Lambda 没有 return 语句,因为已经隐含了 return
(Apple a) -> a.getWeight() > 150 
//返回一个比较结果(boolean),参数为Apple并判断其重量是否超过150
(int x, int y) -> { 
 System.out.println("Result:"); 
 System.out.println(x + y); 
} 
//两个int类型参数,计算并输出其和,没有返回值(void返回)
() -> 42 
//无参数,返回一个int
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()) 
//参数为两个Apple,返回一个int值(两个Apple的重量比较)

具体可以分为六种

//接口定义
public class TestInterface {
    /**
     * 无返回值,无参数
     */
    public interface test1{
        void test();
    }
    /**
     * 无返回值,有一个参数
     */
    public interface test2{
        void test(int a);
    }
    /**
     * 无返回值,有多个参数
     */
    public interface test3{
        void test(int a,int b);
    }
    /**
     * 有返回值,无参数
     */
    public interface test4{
        int test();
    }
    /**
     * 有返回值,有一个参数
     */
    public interface test5{
        int test(int a);
    }
    /**
     * 有返回值,有多个参数
     */
    public interface test6{
        int test(int a,int b);
    }
}

编写测试类

public static void main(String[] args) {
        /**
         * 无返回值,无参数
         */
        TestInterface.test1 t1=()-> System.out.println("无返回值,无参数");
        t1.test();
        /**
         * 无返回值,有一个参数
         */
        TestInterface.test2 t2=(a)-> System.out.println("无返回值,有一个参数。参数为:"+a);
        t2.test(2);
        /**
         * 无返回值,有多个参数
         */
        TestInterface.test3 t3=(a,b)-> System.out.println("无返回值,有多个参数。参数为:"+a+" "+b);
        t3.test(3,4);
        /**
         * 有返回值,无参数
         */
        TestInterface.test4 t4=()->{
            System.out.println("有返回值,无参数。");
            return 4;
        };
        System.out.println(t4.test());
        /**
         * 有返回值,有一个参数
         */
        TestInterface.test5 t5=(a)->{
            System.out.println("有返回值,有一个参数。参数为:"+a);
            return a;
        };
        System.out.println(t5.test(5));
        /**
         * 有返回值,有多个参数
         */
        TestInterface.test6 t6=(a,b)->{
            System.out.println("有返回值,有多个参数。参数为:"+a+" "+b);
            return a+b;
        };
        System.out.println(t6.test(6,6));
    }

输出结果

无返回值,无参数
无返回值,有一个参数。参数为:2
无返回值,有多个参数。参数为:3 4
有返回值,无参数。
4
有返回值,有一个参数。参数为:5
5
有返回值,有多个参数。参数为:6 6
12

2.何时及何地用
可在函数式接口上使用,把 Lambda 表达式作为参数传给实现方法。

函数式接口:只定义一个抽象方法的接口(一个且只有一个)

比如常见的Comparator和Runnable,由于其只有一个抽象方法接口的定义,Lambda表达式会把整个表达式作为函数式接口的实例。

@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2); 
} 
@FunctionalInterface
public interface Runnable { 
void run(); 
} 

@FunctionalInterface:表示该接口会设计成一个函数式接口,简单来说,使用该注解可以保证接口里只允许一个可以实例化的接口。
如果你用@FunctionalInterface定义了一个接口,而它不是函数式接口的话,编译器将返回一个提示原因的错误

示例:

 @FunctionalInterface
    public interface test1{
        //可实例化接口
        void test();
        //接口默认方法实现不算可实例化代码
        default void defaultTest(){

        }
        default void defaultTes2(){

        };
    }

3.付诸实践

读取文件中的内容,使用Lambda表达式来简化操作。

  1. 行为参数化并使用函数式接口来传递行为
@FunctionalInterface
    public interface BufferedReaderProcessor {
    	//接收BufferedReader 返回String
        String process(BufferedReader b) throws IOException;
    }
  1. 执行一个行为
public static String processFile(TestInterface.BufferedReaderProcessor p) throws IOException {
		//将环绕执行(围绕着执行处理的那些重要代码)抽象到此方法
        try (BufferedReader br =
                     new BufferedReader(new FileReader("E:\\study\\Lambda\\src\\main\\java\\data.txt"))) {
            //调用函数式接口处理业务逻辑
            return p.process(br);
        }
    }
  1. 传递Lambda
 public static void main(String[] args) throws IOException {
 		//读取一行内容
        String oneLine =processFile((BufferedReader br) -> br.readLine());
        System.out.println(oneLine);
        //读取两行内容
        String twoLines=processFile((BufferedReader br)->br.readLine()+br.readLine());
        System.out.println(twoLines);
    }

输出结果

在这里插入图片描述

java.util.function 包中的函数式接口

  1. Predicate

接口定义

@FunctionalInterface 
public interface Predicate<T> { 
 //接受泛型T对象,返回boolean值
 boolean test(T t); 
} 

执行行为

public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        //将源集合list中满足p.test条件的元素添加到目标集合并返回
        List<T> results = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)) {
                results.add(t);
            }
        }
        return results;
    }

测试及结果输出

 public static void predicateTest(){
        List<String> listOfStrings=new ArrayList<>();
        listOfStrings.add("string1");
        listOfStrings.add("string2");
        listOfStrings.add("string3");
        listOfStrings.add("string4");
        listOfStrings.add("");
        listOfStrings.add("string5");
        //过滤非空的字符串
        Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
        List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
        System.out.println(nonEmpty);
    }
[string1, string2, string3, string4, string5]
  1. Consumer
    接口定义
@FunctionalInterface 
public interface Consumer<T>{ 
 //消费泛型T对象,无返回值
 void accept(T t); 
} 

执行行为

public static <T> void forEachTest(List<T> list, Consumer<T> c){
        //使用消费者c里面的accept方法来消费源文件list
        for(T i: list){
            c.accept(i);
        }
    }

测试及输出结果

public static void main(String[] args){
        forEachTest(Arrays.asList(1,2,3,4,5),(i)->System.out.println(i));
    }
1
2
3
4
5
  1. Function
    接口定义
@FunctionalInterface 
public interface Function<T, R> { 
//接受泛型T对象,返回泛型R对象
 R apply(T t); 
}

执行行为

//定义枚举类型
public enum Color {
    RED(1,"red"),
    GREEN(2,"green"),
    BLUE(3,"blue");
    int id;
    String color;

    Color(int id, String color) {
        this.id = id;
        this.color = color;
    }
    public int getIdByColor(String color){
        for(Color c : Color.values()){
            if(Objects.equals(c.getColor(), color)){
                return c.getId();
            }
        }
        return 0;
    }
}
//执行行为,将源文件list经过apply方法转换后输出到泛型为R的集合中
public static <T, R> List<R> mapTest(List<T> list, Function<T, R> f) {
        List<R> result=new ArrayList<>();
        for(T t: list){
            result.add(f.apply(t));
        }
        return result;
    }

测试及输出结果

public static void functionTest(){
		//返回枚举类型中所对应的Id
        List<Integer> l = map(Arrays.asList("blue", "red", "green"), (String s) -> Color.BLUE.getIdByColor(s));
        System.out.println(l);
        //Java编译器会会进行类型推断,因此上述代码可不使用String修饰
        //List<Integer> l = map(Arrays.asList("blue", "red", "green"), (s) -> Color.BLUE.getIdByColor(s));

    }
[3, 1, 2]

Java8中常用函数式接口

函数式接口PredicateConsumer
PredicateT -> booleanIntPredicate, LongPredicate, DoublePredicate
ConsumerT -> voidIntConsumer, LongConsumer, DoubleConsumer
Function<T, R>T -> RIntFunction, IntToDoubleFunction, IntToLongFunction, LongFunction,LongToDoubleFunction, LongToIntFunction, DoubleFunction, DoubleToIntFunction, DoubleToLongFunction, ToIntFunction, ToDoubleFunction, ToLongFunction
Supplier() -> TBooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier
UnaryOperatorT -> TIntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator
BinaryOperator(T, T) -> TIntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator
BiPredicate<T, U>(T, U) -> boolean
BiConsumer<T, U>(T, U) -> voidObjIntConsumer, ObjLongConsumer, ObjDoubleConsumer
BiFunction<T, U, R>(T, U) -> RToIntBiFunction<T, U>, ToLongBiFunction<T, U>, ToDoubleBiFunction<T, U>

方法引用

What

方法引用让你可以重复使用现有的方法定义,并像 Lambda 一样传递它们。
方法引用可以被看作仅仅调用特定方法的 Lambda 的一种快捷写法。

Why

在一些情况下,比起使用 Lambda 表达式,可能更易读,更自然。
显式地指明方法的名称,你的代码的可读性会更好

How

目标引用放在分隔符::前,方法的名称放在后面。
例如,Apple::getWeight 就是引用了 Apple 类中定义的方法 getWeight。

Lambda和方法引用实战
  1. 传递代码
//sort方法签名
void sort(Comparator<? super E> c)
//通过实现Comparator进行排序规则定义
public class AppleComparator implements Comparator<Apple> { 
 public int compare(Apple a1, Apple a2){ 
 return a1.getWeight().compareTo(a2.getWeight()); 
 } 
} 
inventory.sort(new AppleComparator()); 
  1. 使用匿名内部类
inventory.sort(new Comparator<Apple>() { 
 //稍微简化操作
 public int compare(Apple a1, Apple a2){ 
 return a1.getWeight().compareTo(a2.getWeight()); 
 } 
}); 
  1. 使用Lambda表达式
inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight())); 
//Comparator 具有一个叫作 comparing 的静态辅助方法,它可以接受一个 Function 来提取 Comparable 键值,并生成一个 Comparator 对象
//所以此操作又可以更简短
inventory.sort(comparing(apple -> apple.getWeight())); 
  1. 方法引用
//最终解决方案
inventory.sort(comparing(Apple::getWeight));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值