Determine the Type#
To customize the collector and implement the Collector interface, you first need to 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>
Implementing 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 the 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, and is 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 the list exists in the map before adding the next element.
The key is obtained by the classifier passed in. The key is obtained through the classifier.
combiner#
Equivalent to the third parameter in reduce, used to merge the generated containers.
@Override
public BinaryOperator<Map<K, List<T>>> combiner() {
return (l,r)->{
l.putAll(r);
return l;
};
}
Simply put the second container into the first one and return it.
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 in which the data is returned.
@Override
public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
}
Related Explanation:
/**
* 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
}