Java 8 Stream Api peek in operation

1 Introduction

I guide uses Java8 Stream API in detail in about the Java 8 Stream API in mapoperation and flatMapdifference operations. Then there is a small partner told me that peekthe operation can also handle element implementation. But you know mapand peekdistinguish it? mapWe already talked about at the beginning of the article, you can go to learn more about it, this article will focus on explaining about peekthe operation.

2. peek

peekThe received operation is a Consumer<T>function. As the name suggests peek operation will follow the Consumer<T>logic functions provided to each element of the consumer flow, while it is possible to change some properties of the interior of the element. Here we would like to mention that Consumer<T>in order to understand what is consumption.

2.1 What is the consumer (Consumer)

package java.util.function;

import java.util.Objects;

 
@FunctionalInterface
public interface Consumer<T> {
 
    void accept(T t);

     // 嵌套accept , 顺序为先执行 accept 后执行参数里的 after.accpet
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
 }
复制代码

Consumer<T>It is a function interface. An abstract method void accept(T t)is intended to accept a Tparameter of type and consumed. In fact, consumption of gave me the feeling is "used up", it is a natural return void. Normally "spent" Tway into two:

  • T itself is void methods typical of that setter.
  • The T interfaces to other methods void (s) is treated , such as we often use a print targetSystem.out.println(T)

2.2 peek operation and demonstration

 Stream<String> stream = Stream.of("hello", "felord.cn");
   stream.peek(System.out::println);
复制代码

If you tested the code given above you will find that they will not run in accordance with the logic. This is for the wisdom of it? This is because the flow of the life cycle has three stages:

  • The initial generation phase.
  • Acquired intermediate operation element one by one and processed. Dispensable. All intermediate operations are inert, and therefore, the flow in the pipeline before any operation flow have no effect.
  • Terminal operation. Usually divided into final consumption ( foreachand the like) are summarized ( collecttwo categories). It is also important that the start operation of the terminal in the pipeline flow stream.

So it should be changed to the following:

 Stream<String> stream = Stream.of("hello", "felord.cn");
  List<String> strs= stream.peek(System.out::println).collect(Collectors.toLIst());
复制代码

For example, the figure below, we give the ball plus a box:

3. peek VS map

peekOperation is generally used when not want to change the type of flow element itself or just internal state of the element; and mapthe elements for changing the type of the stream itself, i.e., derived from another type of operating elements. This is the biggest difference between them. Then peek actual scene in which we used to do? For example, Stream<T>the Ttime of certain properties with a batch peekoperation is more suitable. If we want Stream<T>acquired Twhen using a set of attributes mapit better and better.

4. Summary

We understand that today Stream's peekoperation, but also recalled the Streamlife cycle. Also incidentally to Consumer<T>function were explained. And and mapeach other to do a comparison of their usage scenarios and made a description. I believe that after reading this article you will have a deeper understanding of them.

关注公众号:Felordcn获取更多资讯

Personal blog: https: //felord.cn

Guess you like

Origin juejin.im/post/5ddbe667f265da7e335335ff