【Java8新特性】揭开System.out::printl

【辰兮要努力】:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行!

博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端、后台、数据库、项目案例等相关知识点总结,感谢你的阅读和关注,希望我的博客能帮助到更多的人,分享获取新知,大家一起进步!

吾等采石之人,应怀大教堂之心,愿我们奔赴在各自的热爱里…

一、文章序言

第一次看到这个输出语句还是觉得挺有趣的,本篇文章带你揭开System.out::println的神秘面纱

     List<String> nameList = Arrays.asList("辰兮", "辰兮要努力", "ChenXi");
     nameList.forEach(System.out::println);
复制代码

思考一下这个会输出什么?

对就是正常的遍历输出了集合中每一个具体的对象

辰兮
辰兮要努力
ChenXi
复制代码

二、案例学习

如下的两种方式其实输出是相同的

nameList.forEach(System.out::println);   
复制代码

即调用了某一个对象的某一个方法

nameList.forEach(name -> {
            System.out.println(name);
        });
复制代码

输出结果

辰兮
辰兮要努力
ChenXi
复制代码

就是把你遍历出来的每一个对象都用来去调用System.out的println方法。

System.out::println这段代码其实就是Consumer<T>接口的一个实现方式

image.png

点进去

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

复制代码

我们可以自己创建一个类来模仿System.out::println这样的输出方式

/**
 * 2021-7-25 12:01:54 
 * 辰兮要努力
 */
public class PrintlnDemo {

    /**
     * 创建一个打印的输出方法
     */
    public void printDemo(String a) {
        System.out.println(a);
    }
}

复制代码

案例实践

/**
 * 2021-7-25 12:02:50
 * 辰兮要努力
 */
public class Demo {
    public static void main(String[] args) {
        // 创建出一个数组
        List<String> nameList = Arrays.asList("辰兮", "辰兮要努力", "ChenXi");
        //输出案例
        nameList.forEach(System.out::println);
        //创建一个demo模仿如上的打印输出功能
        nameList.forEach(new PrintlnDemo()::printDemo);
    }
}

复制代码

输出结果

image.png


三、项目实践

创建一个学生类

@Data
public class Student {
    private int age;
    private String name;
}
复制代码

获取list集合里面的某两个属性转换为map集合存储

public class Demo {
    public static void main(String[] args) {

        Student student = new Student(20,"辰兮");
        Student student1 = new Student(22,"辰兮要努力");
        Student student2 = new Student(21,"ChenXi");

        ArrayList<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        list.add(student2);

        //实现List转换为Map
        Map<String,Integer > map = list.stream().collect(Collectors.toMap( Student::getName,Student::getAge));
        System.out.println(map); //{ChenXi=21, 辰兮要努力=22, 辰兮=20}
      
    }
}

复制代码

输出结果

{ChenXi=21, 辰兮要努力=22, 辰兮=20}
复制代码

本方法的业务场景还有很多,我们根据实际的业务进行实践应用即可


非常感谢你阅读到这里,如果这篇文章对你有帮助,希望能留下你的点赞👍 关注❤️ 分享👥 留言💬thanks!!!

2021年11月13日21:34:48 愿我们奔赴在各自的热爱里!