zoukankan      html  css  js  c++  java
  • Lambda表达式

      Lambda 是一个匿名函数,可以把 Lambda表达式 理解为是一段可以传递的代码 (将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升,Lambda表达式是 Java8 中最重要的新功能之一。可以替代只有一个抽象函数的接口实现,告别匿名内部类,代码看起来更简洁易懂。

       Lambda表达式的特点:

      1、函数式编程

      2、参数类型自动推断

      3、代码量少,简洁

      

       Lambda表达式应用场景:
      任何有函数式接口的地方
      函数式接口:定义的接口有且只有一个方法
     

      表达式案例

      ()->{}

      ()->{System.out.println(1);}

      ()->System.out.println(1)

      ()->{return 100;}

      ()->100

      ()->null

      (int x)->{return x+1;}

      (int x)->x+1

      (x)->x+1

      x->x+1

      ()指的是接口里的方法,括号内为参数,参数可以指定类型也可以不指定,因为lambda表达式会做一个自动匹配

      {}为方法的具体实现,如果实现只有一行代码,那么{}也可以省略掉

       如下为直接返回返回值的写法

      ()->100

      ()->null

       Lambda表达式案例:

    /**
             * 创建线程的几种写法
             */
            Runnable rb1 = new Runnable() {
    
                @Override
                public void run() {
                    System.out.println("rb1 run...");
                }
            };
            rb1.run();
    
    
            Runnable rb2 = () -> {
                System.out.println("rb2 run...");
            };
            rb2.run();
    
    
            Runnable rb3 = () -> System.out.println("rb3 run...");
            rb3.run();
    
    
            Callable<String> cb1 = () -> "callable test1";
            System.out.println(cb1.call());
    
            /**
             * 表达式示例
             */
            AnimalFactory af = () -> "rabbit";
            System.out.println(af.produce());
    
    
            AnimalFactory af1 = () -> {
                return "dog";
            };
            System.out.println(af1.produce());
    
    
            StudentDao sd = (stu) -> {
                return true;
            };
    
            Student student = new Student(1, "cat");
            System.out.println(sd.addStu(student));
    
    
            StudentDao sd1 = (stu) -> {
                if (stu.getAge() > 0) {
                    return false;
                }
    
                return true;
            };
            Student student1 = new Student(2, "dog");
            System.out.println(sd1.addStu(student1));
    
    
            StudentDao sd2 = (stu) -> false;
            System.out.println(sd2.addStu(student1));
    public interface AnimalFactory {
    
        public String produce();
    
    }
    public interface StudentDao {
    
        public boolean addStu(Student stu);
    }

      Function函数案例:

      Supplier 代表一个输出 

     

      Consumer 代表一个输入

      BiConsumer 代表两个输入 

     

      Function 代表一个输入,一个输出(一般输入和输出是不同类型的) 

      UnaryOperator 代表一个输入,一个输出(输入和输出是相同类型的) 

     

      BiFunction 代表两个输入,一个输出(一般输入和输出是不同类型的) 

      BinaryOperator 代表两个输入,一个输出(输入和输出是相同类型的)

     

      Function函数<>里的两个类型分别代表输入和输出类型,可通过该函数完成最简单的输入输出操作

         Funcation函数案例

    function函数<>里的两个类型分别代表输入和输出类型,可通过该函数完成最简单的输入输出操作
    
    Function<String, Integer> func = (str)->str.length();
    System.out.println(func.apply("abcd"));
    
    
    Supplier函数<>里的类型代表输出类型,可以通过该函数完成最简单的输出操作
    
    Supplier<Integer> sp = ()->5;
    System.out.println(sp.get());
    
    
    Consumer<>里的类型代表输入类型,可以通过该函数完成最简单的一个参数的输入操作
    
    Consumer<String> cs = (str)->System.out.println(str.length());;
    cs.accept("abc");
    
    
    BiConsumer<>里的类型代表输入类型,可以通过该函数完成最简单的两个参数的输入操作,该方法返回值为void
    
    BiConsumer<String, Integer> bc = (str,num)->System.out.println(str.length()+num);
    bc.accept("kkk", 2);
     /**
             * 函数式接口传参与实现
             */
            Function<String, Integer> func = (str) -> str.length();
            System.out.println(func.apply("abcd"));
    
            Function<String, Integer> func1 = str->str.length();
            System.out.println(func1.apply("qwert")+"啊哈哈");
    
    
            Supplier<Integer> sp = () -> 5;
            System.out.println(sp.get());
    
    
            Consumer<String> cs = (str) -> System.out.println(str.length());
            cs.accept("abc");
    
    
            BiConsumer<String, Integer> bc = (str, num) -> System.out.println(str.length() + num);
            bc.accept("kkk", 2);
    
    
            TestInterface ti = () -> get();
            System.out.println(ti.get());
    
            TestInterface ti1 = () -> 500;
            System.out.println(ti1.get());
    
            BiFunction<String, String, Integer> bf = (a, b) -> a.length() + b.length();
            System.out.println(bf.apply("abc", "uh"));
    public interface TestInterface {
    
        int get();
    }  

      Lambda方法调用:

     

         新建类名:LambdaDemo

      静态方法引用:

    /**
             * 静态方法引用
             */
            Supplier<String> s1 = LambdaDemo::strHandler;
            System.out.println(s1.get());
    
    
            Supplier<String> s2 = Fun::get;
            System.out.println(s2.get());
    
            System.out.println("123");
            System.out.println(get());
    
            Consumer<String> consumer1 = Fun::printStr;
            consumer1.accept("sjias");
    
            Consumer<String> consumer2 = (str)->Fun.printStr(str);
            consumer2.accept("kkkkkk");
    
            Function<String,String> f1 = Fun::toUpperCase;
            System.out.println(f1.apply("nba"));
    
            BiFunction<String,String,Integer> bf1 = Fun::getLength;
            System.out.println(bf1.apply("yu", "uy"));
    
            Consumer<Integer> con1 = (size)->Fun.getSize(size);
            con1.accept(999);
    
            Fun.getSize();

       实例方法引用:

    /**
             * 实例方法
             */
            Supplier<String> sp1 = new LambdaDemo()::put;
            System.out.println(sp1.get());
    
            Supplier<String> sp2 = ()->new LambdaDemo().put();
            System.out.println(sp2.get());
    
            Supplier<String> sp3 = ()->{return new LambdaDemo().put();};
            System.out.println(sp3.get());
    
            Supplier<String> sp4 = new Fun()::print;
            System.out.println(sp4.get());
    
            LambdaDemo ld = new LambdaDemo();
            Function<String,String> up1 = (str)->ld.toUpper(str);
            System.out.println(up1.apply("qwer"));
    
            Function<String,String> up2 = ld::toUpper;
            System.out.println(up2.apply("poi"));
    static int get() {
            return 1;
        }
    
        static String strHandler() {
            return "ok";
        }
    
        public String put(){
            return "put...";
        }
    
        public String toUpper(String str){
            return str.toUpperCase();
        }
    class Fun {
    
        public static String get() {
            return "fuck";
        }
    
        public static void printStr(String str){
            System.out.println(str.substring(0,3));
        }
    
        public static String toUpperCase(String str){
            return str.toUpperCase();
        }
    
        public static Integer getLength(String a,String b){
            return a.length()+b.length();
        }
    
        public String print(){
            System.out.println("Fun print...");
            return "ok";
        }
    
        public static void getSize(int size){
            System.out.println(size);
        }
    
        public static void getSize(){
            System.out.println(666);
        }
    
    }

       对象方法引用:

    class haha{
    
        haha(int age){
            System.out.println("age==" + age);
        }
    
        public Integer fun(String str){
            return str.length();
        }
    
        public void print(){
            System.out.println("haha haha");
        }
    
    }
    
    class hehe{
    
        public Integer fun(String str){
            return str.length();
        }
    
        public void print(){
            System.out.println("hehe hehe");
        }
    
        public void show(String str){
            System.out.println(str);
        }
    }
    /**
             * 对象方法引用lambda表达式
             */
            Consumer<haha> ha = haha::print;
            ha.accept(new haha(12));
    
            Function<String,Integer> f1 = new hehe()::fun;
            System.out.println(f1.apply("bubu"));
    
            BiFunction<hehe,String,Integer> f2 = hehe::fun;
            System.out.println(f2.apply(new hehe(), "tmac-kkk"));
    
            Consumer<hehe> he = hehe::print;
            he.accept(new hehe());
    
            BiConsumer<hehe,String> bc = hehe::show;
            bc.accept(new hehe(),"abc");

       构造方法引用:

            /**
             * 构造函数引用lambda表达式
             */
            Supplier<List> list = ArrayList::new;
            List l1 = list.get();
            l1.add(1);
            l1.add(2);
            System.out.println(l1.size());
    
            Function<Integer,haha> f3 = haha::new;
            f3.apply(34).print();

     

  • 相关阅读:
    SQL Server数据库新建拥有部分查看操作权限的用户
    Asp.net导入Excel数据文件
    前台页面下载服务器端文件
    页面开机自启动,页面置顶显示,页面持续获得焦点,鼠标点击器源码
    asp.net DataGrid GridView 表格之分页显示与翻页功能及自定义翻页页码样式
    asp.net DataGrid GridView 表格之取消设计最初显示的绑定列
    asp.net DataGrid GridView 表格之选中行与获取选中行数据
    Winform 、asp.net TreeView 树形控件
    Torrent种子下载下来的文件,如何校验其完整性?
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
  • 原文地址:https://www.cnblogs.com/jiyukai/p/14805684.html
Copyright © 2011-2022 走看看