zoukankan      html  css  js  c++  java
  • java8Stream map和flatmap的区别

    map和flatmap的区别

    map只是一维 1对1 的映射

    而flatmap可以将一个2维的集合映射成一个一维,相当于他映射的深度比map深了一层 ,

    所以名称上就把map加了个flat 叫flatmap

    flatMap的用法和含义住要通过一个案例来讲解,

    案例:对给定单词列表 ["Hello","World"],你想返回列表["H","e","l","o","W","r","d"]

    第一种方式

            String[] words = new String[]{"Hello","World"};
            List<String[]> a = Arrays.stream(words)
                    .map(word -> word.split(""))
                    .distinct()
                    .collect(toList());
            a.forEach(System.out::print);
    

             代码输出为:[Ljava.lang.String;@12edcd21[Ljava.lang.String;@34c45dca 

                                (返回一个包含两个String[]的list)

            这个实现方式是由问题的,传递给map方法的lambda为每个单词生成了一个String[](String列表)。因此,map返回的流实际上是Stream<String[]> 类型的。你真正想要的是用Stream<String>来表示一个字符串。

            下方图是上方代码stream的运行流程

    第二种方式:flatMap(对流扁平化处理)

            String[] words = new String[]{"Hello","World"};
            List<String> a = Arrays.stream(words)
                    .map(word -> word.split(""))
                    .flatMap(Arrays::stream)
                    .distinct()
                    .collect(toList());
            a.forEach(System.out::print);

    或者:

    String[] words = new String[]{"Hello","World"};
            List<String> collect = Stream.of(words).map(i -> i.split("")).flatMap(Stream::of).collect(toList());
    

    或者:

            List<String> collect = Stream.of(words).flatMap(word -> Stream.of(word.split(""))).collect(toList());
    

      

    结果输出:HeloWrd

            使用flatMap方法的效果是,各个数组并不是分别映射一个流,而是映射成流的内容,所有使用map(Array::stream)时生成的单个流被合并起来,即扁平化为一个流。

        下图是运用flatMap的stream运行流程,

        

  • 相关阅读:
    Objective-C传递数据小技巧
    得到当前活动的controller
    ios7去除手势滑动返回
    生活小常识
    通过email分享
    release下去除nslog宏
    AFNetworking VS ASIHTTPRequest
    web服务器和应用服务器
    mac 搭建git服务器
    UIKit基础:14-序列帧动画的简单介绍
  • 原文地址:https://www.cnblogs.com/wangjing666/p/9999666.html
Copyright © 2011-2022 走看看