What is the equivalent lambda expression for System.out::println

安稳与你 提交于 2019-11-26 05:18:13

The method reference System.out::println will evaluate System.out first, then create the equivalent of a lambda expression which captures the evaluated value. Usually, you would use
o->System.out.println(o) to achieve the same as the method reference, but this lambda expression will evaluate System.out each time the method will be called.

So an exact equivalent would be:

 PrintStream p = Objects.requireNonNull(System.out);
 numbers.forEach(o -> p.println(o));

which will make a difference if someone invokes System.setOut(…); in-between.

It's :

numbers.forEach(i -> {System.out.println(i);});

or even simpler :

numbers.forEach(i -> System.out.println(i));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!