lambda表达式

函数式接口#

  • Supplier 代表一个输出

 

  • Consumer 代表一个输入
  • BiConsumer 代表两个输入

 

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

 

  • BiFunction代表一个输入,一个输出(一般输入和输出是不同类型的)
  • BinaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)
复制代码
public class LambdaTest{
    public static void main(String[] args){
        Runnable runnable = new Runnable(){
            @Override
            public void run(){
                // coding...
            }
        }
        runnable.run();

        Runnable runnable2 = ()->{
            // coding...
        };
        runnable2.run();

        Runnable runnable3 = ()-> // coding
        runnable3.run();

        Callable c1 = new Callable(){
            @Override
            public String call() throws Exception{
                return "";
            }
        };
        c1.call();

        Callable<String> c2 = ()->{return "";};
        c2.call();

        Callable<String> c3 = ()->"";        
        c3.call();

        Function<String, Integer> f1 = (str)->{return str.length();};

        Supplier<String> s1 = ()->{return "";};
        Supplier<String> s2 = ()->"";
        s1.get();

        Consumer<String> c11 = (str)->System.out.println(str);
        c11.accept("sth");

        Runnable runnable1 = ()->{int i = get();System.out.println(i)};
        runnable1.run();

        Runnable runnable2 = ()->{int i = exec();System.out.println(i)};
        runnable2.run();

        Runnable runnable3 = ()->100;
        runnable3.run();

        Runnable runnable4 = ()->"";
        runnable4.run();        

        LamabdInterface li1 = ()->get();
        LamabdInterface li2 = ()->find();
        LamabdInterface li3 = ()->100;
        LamabdInterface li4 = ()->"abc";
        LamabdInterface li5 = ()->true?1:0;

        BiFunction<String, String, Integer> bf = (a,b)->a.length() + b.length();
        bf.apply("", "")

        List<String> list = Arrays.aslist("a", "b", "c");

        for(String s : list){
            System.out.println(s);
        }
        // 上面和下面两种写法都可以输入s
        list.forEach(System.out::println);


    }

    static int get(){
        return 1;
    }
    static int find(){
        return “find";
    }
    static void exec(){
        
    }
}
复制代码
复制代码
public class Test{
    static String put(){
        return "put";
    }
    public static int getSize(int size){
        return size;
    }
    public static void main(String[] args){
        Supplier<String> s1 = ()->Test.put();
        System.out.println(s1.get());

        Supplier<String> s2 = Test::put;
        System.out.println(s2.get());

        Supplier<String> s3 = Fun::test;
        System.out.println(s3.get());        

        Consumer <Integer> c1 = Test::getSize;
        c1.accept("123");

        Consumer <Integer> c2 = (size)->Test::getSize(size);

        Function<String, String> f1 = str->str.toUpperCase();
        Function<String, String> f2 = str->Test.toUpperCase(str);
        Function<String, String> f3 = str->Test::toUpperCase;
        Function<String, String> f4 = Fun::toUpperCase;

        f1.apply("test");
        f2.apply("test");
        f3.apply("test");
        f4.apply("test");

        BiFunction<String, String, Integer> bf = (a,b)->a.length() + b.length();
        BiFunction<String, String, Integer> bf2 = Test::getLength;
        bf.apply("abc", "def")
        bf2.apply("abc", "def")

    }

    class Fun{
        public static String test(){
            return "test";
        }
        public static String toUpperCase(String str){
            return str.toUpperCase();
        }
        public static Integer getLength(String str, String str2){
            return str.length() + str2.length();
        }
    }
}
复制代码

 

posted @   BigBender  阅读(91)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2019-12-31 一元函数微分学概念与计算(一)
2019-12-31 立体几何初步--立体几何体
2019-12-31 高中数学--幂函数
2019-12-31 对数与对数函数
2019-12-31 基本初等函数(Ⅰ)
2019-12-31 高中数学--函数与方程
2019-12-31 高中数学--一次函数和二次函数
点击右上角即可分享
微信分享提示
主题色彩