
1. preface
I'mDetailed usage guide for the Java 8 Stream API It tells Java 8 Stream API in map
operation and flatMap
The difference in operation. Then a friend told me peek
Operations can also implement the processing of elements. but you know map
and peek
Is the difference? map
We have already talked about it in the beginning of the article. You can go and learn about it in detail. This article will focus on explaining it peek
Operation.
2. peek
peek
The operation receives a Consumer<T>
Function. As the name suggests, the peek operation will follow Consumer<T>
The logic provided by the function consumes every element in the flow, while potentially changing some attributes within the element.
Here we want to mention this Consumer<T>
To understand what consumption is.
2.1 What is consumer?
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
//Nested accept , in the order of accept first, and then after.accpet in the parameter is executed
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
Consumer<T>
Is a functional interface. an abstract method void accept(T t)
It means accepting a person T
Parameter of type and consume it. In fact, the feeling that consumption gives me is "used up", and the natural return is void
。Usually "used up" T
There are two ways:
- The void method of T itself The more typical one is
setter
。 - Give T to the void method of other interfaces (classes) for processing For example, we often use printing an object
System.out.println(T)
2.2 peek operation demonstration
Stream<String> stream = Stream.of("hello", "felord.cn");
stream.peek(System.out::println);
If you test the code given above, you will find that it will not follow logic at all. Why is this? This is because the life cycle of a stream has three phases:
- Start the generation phase.
- Intermediate operations retrieve and process elements one by one. It's optional.All intermediate operations are inert, so no operation will have any impact before the flow flows in the pipe.
- Terminal operations. usually divided into the final consumer (
foreach
and the like) and summarized (collect
) Two categories. Another important point is that the terminal operation initiates the flow of flow in the pipeline.
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, in the picture below, we added a box to the sphere:
3. peek VS map
peek
Operations are generally used when you do not want to change the type of the element itself in the stream or only want the internal state of the element; and map
Is used to change the type of the element itself in the stream, that is, the operation of deriving another type from the element. This is the biggest difference between them.
So what scenarios do we actually use peek for? example, for Stream<T>
of T
Used when batch processing certain attributes of peek
Operation is more appropriate. If we're going to start Stream<T>
obtained T
Use when a collection of a certain attribute of map
That would be the best.
4. summary
We understand today Stream
the peek
Operation, and also reviewed Stream
life cycle. Also incidentally, Consumer<T>
Function is explained. and with map
We compared each other and explained their respective usage scenarios. I believe you will have a deeper understanding of them after reading this article.
Comments0