zoukankan      html  css  js  c++  java
  • Java8学习笔记(八)--方法引入的补充

    从形参到实例方法的实参

    示例
    public class Example {
        static List<Integer> intList = Arrays.asList(1,2,3,4);
    
        public String increment(int i)
        {
            return String.valueOf(++i);
        }
        @Test
        public void testLambda()
        {
            //.map(this::increment)<=>.map(i->example.increment(i))...
            intList.stream().map(this::increment).forEach(System.out::println);
        }
    }

    从形参到目标

    示例
    Stream.of("a","b").map(String::toUpperCase).forEach(System.out::println);

    从形参到构造函数实参

    示例
    static List<String> strList = Arrays.asList("a","b","c","d");
    
           //.map(String::new) <=> str->new String(str)
        strList.stream().map(String::new).forEach(System.out::println);

    传递两个形参作为实参

    示例
    List<Integer> intList = Arrays.asList(1,2,3,4);
            //Integer::sum <=> (a,b)->Integer.sum(a,b)
        int i =intList.stream().reduce(Integer::sum).get();
        System.out.println(i);

    第一个形参作为调用的目标而传递

    示例
    //String::concat <=> (a,b)->a.concat(b)
        Stream.of("a","b").reduce("",String::concat);
    岁月本长而忙者自促;天地本宽而卑者自隘;风花雪月本闲,而劳忧者自冗;天行健,君子以自强不息;地势坤,君子以厚德载物;宠辱不惊,闲看庭前花开花落;去留无意,漫随天外云卷云舒.不妄取,不妄予,不妄想,不妄求,与人方便,随遇而安
  • 相关阅读:
    WPF关于“在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。”问题解决办法
    未知的生成错误 因为没有预加载,所以无法解析程序集 GalaSoft.MvvmLight
    C#中的??是什么意思
    WIN10使用管理员权限运行VS2013
    路飞项目
    DRF
    Vue
    dsdffd
    python学习第45天
    python学习第44天
  • 原文地址:https://www.cnblogs.com/vvning/p/7662112.html
Copyright © 2011-2022 走看看