当前位置:   article > 正文

Java8新特性 - Stream - 08 - Stream的map()方法详解_stream.map返回值

stream.map返回值

1.方法介绍

【方法签名】<R> Stream<R> map(Function<? super T, ? extends R> mapper);
  • 1
【方法属性】非终结方法
  • 1
【方法参数】函数式接口 Function,所以可以直接传入一个Lambda表达式
  • 1
【方法作用】将stream的元素进行处理,后返回一个新的stream
  • 1
【方法返回值】 包含新元素的Stream对象
  • 1

2.案例代码

2.1 代码

package com.northcastle.I_stream;

import java.util.ArrayList;
import java.util.stream.Stream;

public class StreamTest07Map {
    public static void main(String[] args) {
        //1.准备一个List集合
        ArrayList<String> list = new ArrayList<>();

        list.add("1");
        list.add("200");
        list.add("300");

        //2.获取Stream对象
        Stream<String> streamList = list.stream();

        //3.将元素映射为另一个元素 : 对字符串进行处理
        streamList.map(s->"map"+s)
                .forEach(System.out::println);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.2 执行结果

在这里插入图片描述

3.完成

Congratulations!
You are one step closer to success!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/495172
推荐阅读
相关标签
  

闽ICP备14008679号