Java的Lambda

目录

1:Lambda的作用

2:jdk1.8的引用

2.1:线程、list、Map练习

2.2:自定义Lambda练习


1:Lambda的作用

Lambda表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。函数式编程能有少写点代码。Lambda依赖以前的匿名内部类的基础,必须是接口,且里边只能有一个方法。我们是函数编程来实现此方法

语法:

(parameters) -> expression

或 (parameters) ->{ statements; }

(参数)-> {表达式 }

2:jdk1.8的引用

2.1:线程、list、Map练习

     /**
     * 线程的匿名内部类
     */
    @Test
    public void t1() {
        new Thread(new Runnable() {
            public void run() {
                System.out.println("线程运行");
            }
        }).start();
    }

    /**
     * 线程的lumbda写法
     */
    @Test
    public void t2() {
        new Thread(() -> System.out.println("Lambda创建线程")
        ).start();
    }


  //jdk 1.8的新特性 list和map的foreach写法
    public static void main(String[] args) {
        List<String> list=new ArrayList<String>();
        list.add("郑州");
        list.add("洛阳");
        list.forEach(a-> System.out.println(a));

        Map<String,String> map=new HashMap<String, String>();
        map.put("郑州","喝牛奶");
        map.put("路","放到");

        map.forEach((key,value)->{
            System.out.println(key+":"+value);
        });


    }

2.2:自定义Lambda练习

定义接口(有没有参数,有没有返回值 四种组合,然后测试)

//没有入参 有返回值
    interface Interface0 {
        int add();
    }

    //没有入参 没有返回值
    interface Interface01 {
        void add();
    }


    //有入参 有返回值
    interface Interface1 {
        int add(int a, int b);
    }

    //有入参没有返回值
    interface Interface2 {
        void add(int a, int b);
    }

    //有入参有返回值 泛型案例
    interface Interface3<T,K> {
        K add(T t,K k);

    }

    //有入参没有返回回值 泛型案例
    public interface Interface4<T,T1 >{
        void add( int t, Long t1);
    }

 测试结果:

  @Test
    public void t3() {
        //匿名内部类
        Interface1 i1 = new Interface1() {
            @Override
            public int add(int a, int b) {
                return 0;
            }
        };

        //标准写法 (参数类型 参数1,参数类型 参数2)-> { return 返回值}
        Interface1 interface1 = (int a, int b) -> {
            return a + b;
        };
        System.out.println("1有返回值Interface1的和,有入参有返回值:" + interface1.add(3, 4));

        //简化写法 参数类型可以不写 return也可以不写 一行代码{}省略 
        interface1 = (c1, c2) -> c1 + c2;//不写return也有返回
        System.out.println("2有返回值Interface1的和,有入参有返回值:" + interface1.add(77, 99));


        Interface2 interface2 = (a, b) -> System.out.println("1无返回值interface2的和,没有入参有返回值:" + (a + b));
        interface2.add(16, 33);

        interface2 = (a1, a2) -> {
            int a = a1 + a2;
            System.out.println("2无返回值interface2的和,没有入参有返回值:" + a);
        };
        interface2.add(5, 9);


        System.out.println("===================");
        Interface3<String, Integer> interface3=(a,b)->{
            if(a.equalsIgnoreCase("郑州")){
                System.out.println(a+"年龄:"+b);
            }
            return 2;
        };
        interface3.add("郑州",50);
        System.out.println("===================");
        Interface0 interface0=()->{
            System.out.println("没有入参 有返回值");
            return 100;
        };

        System.out.println("返回值:"+interface0.add());

        System.out.println("===================");
        Interface01 interface01=()->
            System.out.println("没有入参 没有回值");

       interface01.add();


    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值