【Java8新特性】揭开System.out::println的神秘面纱

【辰兮要努力】: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接口的一个实现方式

在这里插入图片描述
点进去

@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);
    }
}

输出结果
在这里插入图片描述


三、项目实践

创建一个学生类

@Data
public class Student {
    private int age;
    private String name;
}

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

/**
 * 2021-7-25 12:13:35
 * 辰兮要努力
 */
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}

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


分享一下相关文章:秋招Java-面试官就System.out.println()考了我半个小时?
请添加图片描述
相信你一定有所收获我们下期再见!


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

2021年7月25日12:25:25 愿你们奔赴在自己的热爱里!

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员可乐丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值