zoukankan      html  css  js  c++  java
  • JDK8_方法引用

    //方法引用
    public
    class LambdaTest2 { @Test public void test(){ Consumer<String> consumer = new Consumer<String>() { @Override public void accept(String s) { System.out.println("I lived in "+s+" for last year"); } }; consumer.accept("China"); //lambda Consumer<String> consumer1 = s -> System.out.println("I lived in "+s+" for last year"); consumer1.accept("USA"); //方法引用 情况一: 对象 :: 实例方法 PrintStream ps = System.out; Consumer<String> consumer2 =ps::println; consumer2.accept("Germany"); } @Test public void test2(){ Function<Double,Long> function = new Function<Double, Long>() { @Override public Long apply(Double aDouble) { return Math.round(aDouble); } }; System.out.println(function.apply(12.5)); //lambda Function<Double,Long> function1 = (aDouble) -> Math.round(aDouble); System.out.println(function.apply(13.5)); //方法引用 情况二: 类 :: 静态方法 Function<Double,Long> function2 =Math::round; System.out.println(function.apply(14.5)); } @Test public void test3(){ //原来的写法 Comparator<Integer> comparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }; System.out.println(comparator.compare(12,32)); //lambda Comparator<Integer> comparator1 = (t1,t2) ->t1.compareTo(t2); System.out.println(comparator1.compare(65,34)); //方法引用 情况三: 类 :: 实例方法 //Comparator<Integer> comparator2 =Integer::compareTo; Comparator<Integer> comparator2 =Integer::compare; System.out.println(comparator2.compare(55,65)); } }
  • 相关阅读:
    菜鸟之旅——序章0
    Nginx设置反向代理内网服务器/内部端口
    Laradock ppa加速
    Qt setGraphicsEffect出现崩溃 读取位置 时发生访问冲突
    3.5tensorflow线性回归和线性拟合
    Dockerfile Security Best Practice
    Docker: Exec user process caused "no such file or directory"
    Installing Kubernetes with kops
    Dockerfile 最佳实践
    Configuring HSTS in NGINX
  • 原文地址:https://www.cnblogs.com/Anonymity-zhang/p/14487396.html
Copyright © 2011-2022 走看看