简体   繁体   English

java中的平面图:真的需要lambda吗?

[英]Flatmap in java: is the lambda really necessary?

Here is a code sample (form Mykong.com):这是一个代码示例(来自 Mykong.com):

String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};

//Stream<String[]>
Stream<String[]> temp = Arrays.stream(data);

//Stream<String>, GOOD!
Stream<String> stringStream = temp.flatMap(x -> Arrays.stream(x));

If I understand it correctly, the lambda here is doing the actual flattening but could not flatMap understand what needs to do the flattening so that the lambda could be ommitted?如果我理解正确,这里的 lambda 正在执行实际的展平,但 flatMap 无法理解需要进行展平以便可以省略 lambda?

EDIT: Could flatMap have a default lambda used to convert in the "intuitive" way?编辑: flatMap 是否可以使用默认的 lambda 来以“直观”的方式进行转换?

The lambda is not required, you can accomplish the same thing via a method reference.不需要 lambda,您可以通过方法引用完成同样的事情。 In practice the two approaches are identical though.在实践中,这两种方法是相同的。

The last line could be:最后一行可能是:

Stream<String> stringStream = temp.flatMap(Arrays::stream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM