zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    论读书
    睁开眼,书在面前
    闭上眼,书在心里
  • 相关阅读:
    手动实现 SpringMVC
    2014年9月9日 高级命令command的使用(上)
    2014年8月29日 透视图补充及视图开头
    2014年8月24日 菜单 工具条 右键菜单(上下文菜单)
    2014年8月14日 透视图
    2014年8月8日
    2014年8月1日
    关于EMF中从schema到ecore转变中的默认处理问题
    JAVA一些常用的时间操作
    echarts基本使用
  • 原文地址:https://www.cnblogs.com/YC-L/p/14214333.html
Copyright © 2011-2022 走看看