Determine the type#
To customize the collector, you need to implement the Collector interface, and first determine the type:
- The type of the elements to be collected
- The type of the accumulator/accumulate
- The type of the final result
Suppose you want to implement such a collector:
public class GroupingBy<T,K> implements Collector<T,Map<K,List<T>>,Map<K,List<T>>>
The types are:
- T
- Map<K,List>
- Map<K,List>
Implement the components of the collector#
The collector has 4 important components, which are all functions:
- supplier
- accumulator
- combiner
- finisher
supplier#
The supplier is used to create a container.
@Override
public Supplier<Map<K, List<T>>> supplier() {
return ()-> new HashMap<>();
}
The accumulator is the accumulator, which is equivalent to the second parameter in reduce, used to add the next content to the previous result.
@Override
public BiConsumer<Map<K, List<T>>, T> accumulator() {
return (accumulator,ele)->{
K key = this.classifier.apply(ele);
List<T> tList = accumulator.get(key);
if (tList == null){
tList = new ArrayList<>();
}
tList.add(ele);
accumulator.put(key,tList);
};
}
Check if there is a list in the map before adding the next element.
The key is obtained by the classifier passed in, and the key is obtained through the classifier.
combiner#
Equivalent to parameter 3 of reduce, used to merge the generated containers.
@Override
public BinaryOperator<Map<K, List<T>>> combiner() {
return (l,r)->{
l.putAll(r);
return l;
};
}
Just put the latter into the former and return.
finisher#
Describes the final result.
@Override
public Function<Map<K, List<T>>, Map<K, List<T>>> finisher() {
return accumulator->accumulator;
}
Additional characteristics#
Describes the form of the returned data.
@Override
public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
}
Related explanations:
/**
* Characteristics indicating properties of a {@code Collector}, which can
* be used to optimize reduction implementations.
*/
enum Characteristics {
/**
* Indicates that this collector is <em>concurrent</em>, meaning that
* the result container can support the accumulator function being
* called concurrently with the same result container from multiple
* threads.
*
* <p>If a {@code CONCURRENT} collector is not also {@code UNORDERED},
* then it should only be evaluated concurrently if applied to an
* unordered data source.
*/
CONCURRENT,
/**
* Indicates that the collection operation does not commit to preserving
* the encounter order of input elements. (This might be true if the
* result container has no intrinsic order, such as a {@link Set}.)
*/
UNORDERED,
/**
* Indicates that the finisher function is the identity function and
* can be elided. If set, it must be the case that an unchecked cast
* from A to R will succeed.
*/
IDENTITY_FINISH
}