我对Java Stream.flatMap的理解正确吗? [英] Is my understanding of Java Stream.flatMap correct?

查看:70
本文介绍了我对Java Stream.flatMap的理解正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图回答这个问题,但是我做到了不是因为我对Streams不够了解.请告诉我我的解释是否正确.

I was trying to answer this question, but I did not because I don't understand Streams well enough. Please tell me if my explanation is correct or not.

我的答案:

import java.util.Arrays;
import java.util.stream.Stream;

public class Temp {
    public static void main(String [] args){
        String [] input = {"1,2", "3,4", "5"};
        String [] expected = {"1", "2", "3", "4", "5"};

        String [] actual = Stream.of(input)
                .flatMap(s -> Arrays.stream(s.split(",")))
                .toArray(String [] :: new);

        //Testing - Runs only when assertions are enabled for your JVM. Set VM args = -ea for your IDE.
        assert Arrays.equals(actual, expected) : "Actual array does not match expected array!";
    }
}

我的解释:

1-获取元素流(在此示例中为Strings),然后一次将一个元素传递给flatMap.

1 - Take a stream of elements (Strings in this example) and pass one element at a time to flatMap.

问题-实际上一次是一个元素吗?

2-flatMap使用Function,它将元素转换为Stream.在示例中,该函数采用一个字符串("1,2"),并将其转换为多个字符串("1","2")的流.多个字符串流是由Arrays.stream(一个数组)生成的,我们知道它接受一个数组并将其转换为流.该数组由s.split(",")生成.所有其他元素都将被处理并放入此流中.

2 - flatMap takes a Function which converts an element into a Stream. In the example, the function takes a String ("1,2") and converts it into a stream of multiple Strings ("1", "2"). The stream of multiple strings is generated by Arrays.stream(an array) which we know takes an array and converts it into a stream. That array was generated by s.split(","). All other elements are processed and put into this one stream.

问题- flatMap是否为输入数组中的所有元素返回一个Stream或为输入数组中的每个元素返回一个Stream?

3-toArray将元素从flatMap获取的单个流中,并将它们放入一个数组中.

3 - toArray takes elements in the single stream it got from flatMap and puts them into one array.

推荐答案

平面地图的预定义语法是

Predefined syntax of flat map is

<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)

where, R is the element type of the new stream.
Stream is an interface and T is the type 
of stream elements. mapper is a stateless function 
which is applied to each element and the function
returns the new stream.

那么flatMap在内部如何工作?

so how flatMap works internally?

它首先将返回另一个Optional的函数应用于内部的对象(如果存在),然后在返回结果之前对其进行展平,因此您不必自己进行操作.

内部定义了这样的内容

public static <T> Optional<T> flatten(Optional<Optional<T>> optional) {
    return optional.orElse(Optional.empty());
}

所以在你的情况下

String [] actual = Stream.of(input)
            .flatMap(s -> Arrays.stream(s.split(",")))
            .toArray(String [] :: new);

StreamofInput(input)  - taking input as collection
flatMap: adding map function
s-> Arrays.stream(s.split(","))- taking argument from your arrays individually which is represent by "s" , then converting your array which is splitted by "," and converting into stream.
toArray : converting you result into flat single array because its stateless so it will give you new collection.

有关更多信息,您可以访问此处

for more you can visit here

这篇关于我对Java Stream.flatMap的理解正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆