20220403 java学习笔记-------lambda表达式

lambda“λ”:表示匿名函数

格式:()->结果表达式

参数->结果表达式

(参数1,参数2,参数3)->结果表达式

使用范围:

1)函数时接口

2)实现无参抽象方法

3)实现有参抽象方法

4)使用代码块:会自动判断返回值类型是否符合抽象方法的定义

public class GradeDemo {
    public static void main(String[] args) {
       CheckGade g=(n)->{
           if(n>=90&&n<=100){
               return "成绩优秀";
           }else if(n>=80&&n<90) {
               return "成绩为良";
           }else  if(n>=60&&n<80){
               return "成绩为中";
           }else if(n>=0&&n<60) {
               return "成绩为差";
           }else {
               return "成绩无效";
           }
       } ;
        System.out.println(g.check(89));
    }
}
interface CheckGade{
    String check(int grade);
}

5)lambda表达式无法更改局部变量

6)lambda表达式可以修改成员变量

7)lambda表达式用于异常

import java.util.Scanner;

public class ThrowExceptionDemo {
    public static void main(String[] args) {


        //lambda表达式创建 AntiaddictInterface对象
        AntiaddictInterface ai = (a) -> {
            if (a < 18) {
                throw new UnderAgeEXception("未满18周岁,开始防沉迷模式");
            } else {
                return true;
            }
        };
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年龄:");
        int age=sc.nextInt();
        
        try {
            if(ai.check(age)){
                System.out.println("欢迎进入XX世界");
            }
            
        }catch (UnderAgeEXception e){
            System.out.println(e);
        }
        sc.close();
    }
}
interface  AntiaddictInterface{
    boolean check(int age)throws UnderAgeEXception;
}
class UnderAgeEXception extends Exception{
    public UnderAgeEXception(String message){
        super(message);
    }
}

8)引用静态方法

语法:类名::静态方法名

9)引用成员变量

语法: 对象名::成员方法名

10)引用带泛型的方法

import java.util.HashSet;

public class ParedigmDemo {
    //静态方法,使用泛型参数,在方法名之前定义泛型。此方法用于数组中查找重复元素个数
    public static<T> int repeatCoount(T[] t){
        int arrayLength=t.length;
        HashSet<T>set =new HashSet<>();
        for(T tmp:t){
            set.add(tmp);
        }
        return arrayLength-set.size();

    }
    public static void main(String[] args) {
       Integer a[] ={1,2,3,4,6,74,5,5};
       String s[] ={"王","李","赵","牛","韩","王"};
       //创建接口对象,integer作为泛型,引入ParedigmDemo类的静态方法,方法名要定义泛型
        ParedigmIterface<Integer> p1=ParedigmDemo::<Integer>repeatCoount;
        System.out.println("整数数组重复元素个数:"+p1.method(a));

        ParedigmIterface<String> p2=ParedigmDemo::<String>repeatCoount;
        System.out.println("字符串数组重复元素个数:"+p2.method(s));


    }
}
interface   ParedigmIterface<T>{
    int method(T[] t);
}

11)引用无参构造方法:

语法:类名::new 

12)引用数组构造方法

语法:类名[]::new

/**
 * @author 龙御修
 * @create 2022-04-03 16:32
 */
public class ArraysConsDemo {
    public static void main(String[] args) {
        ArraysConsinterface<ArraysConsDemo[]>a=ArraysConsDemo[]::new;
        ArraysConsDemo array[]=a.action(3);//接口创建数组,并指定素组个数
        array[0]=new ArraysConsDemo();
        array[1]=new ArraysConsDemo();
        array[2]=new ArraysConsDemo();
    }
    
}
interface  ArraysConsinterface<T>{
    T action(int n);
}

 13)Fuction接口方式

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royalreairman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值