• 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);
    岁月本长而忙者自促;天地本宽而卑者自隘;风花雪月本闲,而劳忧者自冗;天行健,君子以自强不息;地势坤,君子以厚德载物;宠辱不惊,闲看庭前花开花落;去留无意,漫随天外云卷云舒.不妄取,不妄予,不妄想,不妄求,与人方便,随遇而安
  • 相关阅读:
    199. 二叉树的右视图
    二叉树前、中、后、层次、遍历的非递归法
    奇思妙想
    917. 仅仅反转字母【双指针】
    JVM性能监控与故障处理工具
    Java线程池使用和常用参数(待续)
    MySQL常用知识
    手写常用算法
    LightOj 1170
    逆元总结
  • 原文地址:https://www.cnblogs.com/vvning/p/7662112.html
Copyright © 2020-2023  润新知