//Stream<T> peek(Consumer<? super T> action);
//peek接受一个Consumer参数,没有返回值,因此不会改变原流元素(对象元素除外)
Stream.of("one", "two", "three", "four")
.peek(String::toUpperCase)
.forEach(System.out::println);
//<R> Stream<R> map(Function<? super T, ? extends R> mapper);
//map接受一个Function参数,有返回值,将更改流中元素
Stream.of("one", "two", "three", "four")
.map(String::toUpperCase)
.forEach(System.out::println);
//result:
//one
//two
//three
//four
//ONE
//TWO
//THREE
//FOUR
stream peek与map的区别
最新推荐文章于 2023-12-29 10:03:21 发布