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));
    }
  • 相关阅读:
    Atitit.eclise的ide特性abt 编译
    Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit.eclipse 4.3 4.4  4.5 4.6新特性
    atitit.错误:找不到或无法加载主类 的解决 v4 qa15.doc
    Atitit RSA非对称加密原理与解决方案
    Atitti.数字证书体系cer pfx attilax总结
    Atitit ftp原理与解决方案
    Atitit qzone qq空间博客自动点赞与评论工具的设计与实现
    Atitit 软件国际化原理与概论
  • 原文地址:https://www.cnblogs.com/maohuidong/p/14072565.html
Copyright © 2011-2022 走看看