zoukankan      html  css  js  c++  java
  • Optional和Stream的map与flatMap

    Optional的map和flatMap

    Optional存在map和flatMap方法。map源码如下

        public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
            Objects.requireNonNull(mapper);
            if (!isPresent())
                return empty();
            else {
                return Optional.ofNullable(mapper.apply(value));
            }
        }
    

    map方法和Stream的map方法类似,都是对变量进行映射转换。

    Optional.flatMap源码如下:

    public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return Objects.requireNonNull(mapper.apply(value));
        }
    }
    

    从源码看,map和flatMap均要求optional对象非空才执行mapper方法,二者均返回Optional对象。但是map会自动将计算结果封装为Optional对象,而flatMap需要自己进行封装。

    Stream的map和flatMap

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

    map方法好说,是一个很简单的Function方法,输入参数类型?super T,输出参数类型为? extends R,是一种一对一的关系。将一个流映射成另外一个流。

    假设需要将list的每个字符串都映射成为大写,

    List<String> list = Arrays.asList("hello world","hello world");
    list.stream().map(String::toUpperCase).forEach(s -> System.out.print(s+"8 "));
    //HELLO WORLD8 HELLO WORLD8 
    

    若现在需求变了,需要将list的每个字符串按照“ ”分割,分别处理每个单词(list共4个单词)。这里我们仍然使用map进行测试。

    list.stream().map(s -> Stream.of(s.split(" "))).map(String::toUpperCase).forEach();
    

    该代码存在语法错误,第二个map提示所需参数为Function<? super Stream, ? extend R> mapper。这说明第一个map产生了多个流(hello流和world流),这个时候就需要使用到flatMap了。jdk8对flatMap的解释为

    Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

    意思很明确,流的每个元素都会产生一个新的流。这多个流会合并为一个流,作为flatMap的返回结果。

    list.stream().flatMap(s -> Stream.of(s.split(" "))).map(String::toUpperCase).forEach(s -> System.out.println(s+"9 "));
    //output:HELLO9 WORLD9 HELLO9 WORLD9 
    
  • 相关阅读:
    java集合
    linux网络配置(iproute2)
    linux网络配置(ifcfg)
    linuxshell编程之数组和字符串处理工具
    linux网络配置文件
    linux 内核版本和发行版本区别
    linux 相关零碎知识整理
    linux计算机网络基础
    linux bash编程之函数和循环控制
    linux bash shell编程之参数变量和流程控制。
  • 原文地址:https://www.cnblogs.com/ilovena/p/10461529.html
Copyright © 2011-2022 走看看