Common methods in Stream _map

Mapping: map:
If you need to map elements in a stream to another stream, you can use the map method

<R> Stream<R> map(Function<? super T,? extends R> mapper);
该接口需要一个Function函数式接口参数,可以将当前流中的T类型数据转换为另一种R类型的流
Function中的抽象方法:
    R apply(T t);

Example:

public class Demo04Stream_map {
    
    
    public static void main(String[] args) {
    
    
        //获取一个String 类型的Stream流
        Stream<String> stream = Stream.of("1", "2", "3", "4");
        //使用map方法,把字符串类型的整数,转换(映射)为Integer类型的整数
       Stream<Integer> stream2 = stream.map(s-> Integer.parseInt(s));
        //遍历stream2流
        stream2.forEach(i-> System.out.println(i));
    }
}

In this code, the parameters of the map method are referenced by the method, and the string type is converted into an int type (and automatically boxed into an Integer class
object).
Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109154841