public static Collector<CharSequence, ?, String> joining(CharSequence delimiter) {
return joining(delimiter, “”, “”);
}
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix) {
return new CollectorImpl<>(
() -> new StringJoiner(delimiter, prefix, suffix),
StringJoiner::add, StringJoiner::merge,
StringJoiner::toString, CH_NOID);
}
- Supplier< A> supplier()
其函数为() -> new StringJoiner(delimiter, prefix, suffix),累积器的初始值为StringJoiner。
- BiConsumer<A, T> accumulator
累积器:StringJoiner::append,即会对流中的元素执行追加。
- BinaryOperator< A> combiner
组合器,StringJoiner::merge。
- Function<A,R> finisher
转换器:由于累积器返回的最终对象为StringBuilder,并不是目标String类型,故需要调用StringBuilder#toString方法进行转换
- Set< Characteristics> characteristics
无任何行为。
其示例如下:
聚合相关收集器,主要包括minBy、maxBy、sum、avg等相关函数,其主要方法声明如下:
public static Collector<T, ?, Optional> minBy(Comparator<? super T> comparator)
public static Collector<T, ?, Optional> maxBy(Comparator<? super T> comparator)
public static Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)
public static Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper)
public static Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)
public static Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)
public static Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)