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

     

    map和flatmap的区别

    map只是一维 1对1 的映射

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

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

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

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

    第一种方式

    1
    2
    3
    4
    5
    6
    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(对流扁平化处理)

    1
    2
    3
    4
    5
    6
    7
    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);

    或者:

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

    或者:

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

      

    结果输出:HeloWrd

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

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

        

     测试代码:

    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello","world");
        String[] words = new String[]{"Hello","World"};
        //List<String[]> temp = list.stream().map(s -> s.split("")).distinct().collect(Collectors.toList());
        List<String[]> temp = Stream.of(words).map(s -> s.split("")).distinct().collect(Collectors.toList());
        System.out.println(JSON.toJSONString(temp));
    
        //List<String> temp2 = list.stream().map(s->s.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        List<String> temp2 = Arrays.stream(words).map(s -> s.split("")).flatMap(Stream::of).distinct().collect(Collectors.toList());
        System.out.println(JSON.toJSONString(temp2));
    }
  • 相关阅读:
    ti8168 eth0 启动
    MVC 5 Ajax + bootstrap+ handle bar 例: 实现service 状态
    MinGW 介绍
    VC POST表单——登录验证新浪邮箱
    关于ActionContext.getContext()的使用方法心得
    java对象数组
    播放音乐案例
    service
    监听短信(监听系统广播)
    普通广播接收者和有序广播接收者
  • 原文地址:https://www.cnblogs.com/maohuidong/p/14072565.html
Copyright © 2011-2022 走看看