Java基础总结(二)Lambda

本文介绍了Java中的Lambda表达式和匿名类的使用,包括函数式接口的概念,以及Lambda的各种参数和返回值类型的写法。同时,展示了匿名类的五种不同写法,包括Lambda简化表达和利用已有的工具方法。文章还通过MathOperation接口展示了如何进行数学运算,并使用泛型确保返回值兼容多种类型。
摘要由CSDN通过智能技术生成

Java基础总结(二)Lambda

1、简单写法

2、匿名类的几种写法

1、简单写法

1-函数式接口:一个接口只存在一个API
2-参数从无到多个的写法
	interface LambdaTest {
        // 1、无参数有返回值
//        int returnInt ();
        // 2、有1个参数返回值
//        int returnInt2(int x);
        // 3、有两个参数有返回值
//        int returnInt3(int x, int y);
        // 4、参数为String对象,无返回值
        void objectTest(String str);
    }
    public static void main(String[] args) {
        LambdaDemo demo = new LambdaDemo();
        // 1、无参数有返回值
//        LambdaTest lambdaTest1 = () -> 5;
        // 2、有1个参数返回值
//        LambdaTest lambdaTest2 = (x) -> 5 * x;
        // 3、有两个参数有返回值
//        LambdaTest lambdaTest3 = (x, y) -> 5;
        // 4、参数为String对象,无返回值
//        LambdaTest lambdaTest4 = (s) -> System.out.println(s);
//        LambdaTest lambdaTest44 = System.out::println;
	}

2、匿名类的几种写法

 	package gk.frame.lombda.two;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;

/**
 * @author tangjiawen
 * @version 1.0
 * @Desc 伪范型:编译后的字节码里面没有范型这个东西
 * 擦除法:for循环里面先检查类型,然后范型的相关信息全被擦除
 * 范型约束: 让T继承指定接口,则使用时必须满足是这些接口的子类型才可以传递参数
 */
public class LambdaDemo<T extends Serializable & Comparable & Collection> {
    public static void main(String[] args) {
        LambdaDemo demo = new LambdaDemo();

        // 2、匿名类的几种写法
        // 写法1 new匿名类
        MathOperation<Integer> operation1 = new MathOperation<Integer>() {
            @Override
            public Integer operation(int a, int b) {
                return 1;
            }
        };
        MathOperation<Integer> operation11 = new MathOperation<Integer>() {
            @Override
            public Integer operation(int a, int b) {
                return a + b;
            }
        };
        // 写法2 Lambda-无类型参数,显示指定返回值类型
        MathOperation operation2 = (a, b) -> 1;
        // 写法3 Lambda-有类型参数,间接制定了返回值类型
        MathOperation operation3 = (int a, int b) -> a + b;
        // 写法4 Lambda 超级简约写法,用已有的工具类 + :: 代替实现方式
        System.out.println("operation5 : " + operation3.operation(1, 2));

        // 写法5 Lambda 利用返回值或者叫业务代码来决定返回值类型
        MathOperation addition = Integer::sum;
        MathOperation subtraction = (int a, int b) -> a - b;
        MathOperation multiplication = (int a, int b) -> a * b;
        MathOperation division = (int a, int b) -> a / b;
        MathOperation power = Math::pow;

        System.out.println("10 + 5 = " + demo.operate(10, 5, addition));
        System.out.println("10 - 5 = " + demo.operate(10, 5, subtraction));
        System.out.println("10 x 5 = " + demo.operate(10, 5, multiplication));
        System.out.println("10 / 5 = " + demo.operate(10, 5, division));
        System.out.println("10 ^ 5 = " + demo.operate(10, 5, power));

        // 写法6 自定义写法
        SelfPrint selfPrint = message -> {
            System.out.println(message);
        };
        // 当内部函数的参数跟接口的参数一样时,可以省略内部类,直接用::引用内部方法
        SelfPrint selfPrint2 = System.out::println;

        // 写法7 forEach简写省略参数
//        Arrays.asList(1,2,3,4,5,6).forEach( x -> System.out.println(x));
        // 同上一样,遍历对象为单一参数的函数可以这么写
        Arrays.asList(1,2,3,4,5,6).forEach(System.out::println);
        // 遍历对象还要做业务操作+1,所以不能简写
//        Arrays.asList(1,2,3,4,5,6).forEach( x -> System.out.println(x + 1));
    }

    /**
     * 函数式接口
     *
     * @param <T> 范型T 为了返回值可以兼容各种类型
     */
    interface MathOperation<T> {
        T operation(int a, int b); // 返回类型+函数名+参数类型的列表
    }

    /**
     * @param a             参数 满足内部传参
     * @param b             参数 满足内部传参
     * @param mathOperation 函数式接口
     * @param <T>           表示这个方法可以返回多种不同类型的返回值
     * @return
     */
    private <T> T operate(int a, int b, MathOperation<T> mathOperation) {
        return mathOperation.operation(a, b);
    }

    interface SelfPrint {
        void selfPrint(String message);
    }
}

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值