newList = new ArrayList<>(left.size() + right.size());
newList.addAll(left);
newList.addAll(right);
return newList;
default:
left.addAll(right);
return left;
}
};
return new CollectorImpl<>(Collections::emptyList, accumulator, combiner);
}
/**
* Returns a {@code Collector} that accumulates the input elements into a
* new {@code Set}. There are no guarantees on the type, mutability,
* serializability, or thread-safety of the {@code Set} returned.
*
* This is an {@link Collector.Characteristics#UNORDERED unordered}
* Collector.
*
* @param the type of the input elements
* @return a {@code Collector} which collects all the input elements into a
* {@code Set}
*/
public static
Collector> toSet() {
return new CollectorImpl<>((Supplier>) HashSet::new,
(r, t) -> { r.add(t); return r; },
(r1, r2) -> { r1.addAll(r2); return r1; },
CH_STRICT_UNORDERED);
}
/**
* Returns a {@code Collector} that concatenates the input elements into a
* new {@link StringBuilder}.
*
* @return a {@code Collector} which collects String elements into a
* {@code StringBuilder}, in encounter order
*/
public static Collector toStringBuilder() {
return new CollectorImpl<>(StringBuilder::new,
(r, t) -> { r.append(t); return r; },
(r1, r2) -> { r1.append(r2); return r1; },
CH_STRICT);
}
/**
* Returns a {@code Collector} that concatenates the input elements into a
* new {@link StringJoiner}, using the specified delimiter.
*
* @param delimiter the delimiter to be used between each element
* @return A {@code Collector} which collects String elements into a
* {@code StringJoiner}, in encounter order
*/
public static Collector toStringJoiner(CharSequence delimiter) {
BinaryOperator merger = (sj, other) -> {
if (other.length() > 0)
sj.add(other.toString());
return sj;
};
return new CollectorImpl<>(() -> new StringJoiner(delimiter),
(r, t) -> { r.add(t); return r; },
merger, CH_STRICT);
}
/**
* {@code BinaryOperator