Custom Collector Collector

1. Customize the collector. Sometimes the collectors of the Collectors class cannot meet the needs. We need to customize the collector. Next, let’s define a collector. First, let’s look at the methods of the Collector interface: supplier(), accumulator(), combiner(), finisher(), characteristics(). From the method, you probably also know the role of each method. The role of the collector is to do some reduction operations.
1. supplier() returns a Supplier function interface, which mainly provides examples of operations.
2. accumulator() returns the BiConsumer function interface, which is mainly the calculation logic of consuming objects or processing objects. This is the main method.
3. combiner() returns a BinaryOperator function interface, which is a reduction operation that reduces or integrates the data processed by the accumulator() method.
4. finisher() returns a Function function interface, and its function is to convert the stream reduced by combiner() into another type of stream.
5. characteristics() returns the set collection, marking some characteristics of this collector.
2. We implement a simple odd-even collector. Of course, you can implement more complex logic. We just show how to customize the collection.
The code is as follows:
insert image description here
insert image description here
The test class code is as follows:
insert image description here
insert image description here
insert image description here
The above is the use of the custom collector.

Guess you like

Origin blog.csdn.net/u013326684/article/details/102708358