1 Introduction
I guide uses Java8 Stream API in detail in about the Java 8 Stream API in map
operation and flatMap
difference operations. Then there is a small partner told me that peek
the operation can also handle element implementation. But you know map
and peek
distinguish it? map
We already talked about at the beginning of the article, you can go to learn more about it, this article will focus on explaining about peek
the operation.
2. peek
peek
The 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 T
parameter of type and consumed. In fact, consumption of gave me the feeling is "used up", it is a natural return void
. Normally "spent" T
way 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 target
System.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 (
foreach
and the like) are summarized (collect
two 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
peek
Operation is generally used when not want to change the type of flow element itself or just internal state of the element; and map
the 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 T
time of certain properties with a batch peek
operation is more suitable. If we want Stream<T>
acquired T
when using a set of attributes map
it better and better.
4. Summary
We understand that today Stream
's peek
operation, but also recalled the Stream
life cycle. Also incidentally to Consumer<T>
function were explained. And and map
each 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获取更多资讯