public class MoreCollectors extends Object
Useful collectors which aren’t included as Java 8 built-ins.
| Constructor and Description |
|---|
MoreCollectors() |
| Modifier and Type | Method and Description |
|---|---|
static String |
codePointsToString(IntStream codePoints)
Converts a stream of unicode code points into a String.
|
static <T> Collector<T,?,Optional<T>> |
singleOrEmpty()
Collector which traverses a stream and returns either a single element (if there was only one element) or empty (if there were 0 or more than 1 elements).
|
static <T> Optional<T> |
singleOrEmptyShortCircuiting(Stream<T> stream)
Same behavior as
singleOrEmpty(), except that it returns early if it is possible to do so. |
public static <T> Collector<T,?,Optional<T>> singleOrEmpty()
Collector which traverses a stream and returns either a single element (if there was only one element) or empty (if there were 0 or more than 1 elements). It traverses the entire stream, even if two elements have been encountered and the empty return value is now certain.
Implementation credit to Misha on StackOverflow.
public static <T> Optional<T> singleOrEmptyShortCircuiting(Stream<T> stream)
Same behavior as singleOrEmpty(), except that it returns early if it is possible to do so. Unfortunately, it is not possible to implement early-return behavior using the Collector interface, so MoreCollectors takes the stream as an argument.
Implementation credit to Thomas Jungblut on StackOverflow.