Class Joiner
- All Implemented Interfaces:
Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
Collector) that joins strings.
When used to join a pair of objects (for example in a BiStream), it can be used for BiFunction:
BiStream.zip(userIds, names)
.mapToObj(Joiner.on('=')::join) // (id, name) -> id + "=" + name
....;
Alternatively, it can be used as a Collector. For example,
names.collect(Joiner.on(',')) is equivalent to names.collect(Collectors.joining(",")).
Except that JDK joining() requires the inputs to be strings; while Joiner can join any input,
e.g. numbers.
Starting from v9.6, Joiner.on() is optimized to be more efficient than
Collectors.joining() when the input has only one string element because it will return the
string element as is, whereas JDK Collectors.joining() delegates to StringJoiner,
which performs a deep copy even when there is only one string with no prefix and suffix.
You can also chain between(char, char) to further enclose the joined result between a pair of strings.
The following code joins a list of ids and their corresponding names in the format of
"[id1=name1, id2=name2, id3=name3, ...]" by first joining each pair with '=' and then
joining the "id=name" entries with ',', finally enclosing them between
'[' and ']':
BiStream.zip(userIds, names)
.mapToObj(Joiner.on('=')::join)
.collect(Joiner.on(", ").between('[', ']'));
Which reads clearer than using JDK Collectors.joining():
BiStream.zip(userIds, names)
.mapToObj(id, name) -> id + "=" + name)
.collect(Collectors.joining(", ", "[", "]"));
If you need to skip nulls and/or empty strings while joining, use
collect(Joiner.on(...).skipNulls()) or collect(Joiner.on(...).skipEmpties()) respectively.
Unlike Guava com.google.common.base.Joiner, nulls don't cause NullPointerException,
instead, they are stringified using String.valueOf(java.lang.Object).
- Since:
- 5.6
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Collector
Collector.Characteristics -
Method Summary
Modifier and TypeMethodDescriptionBiConsumer<com.google.mu.util.stream.Joiner.FasterStringJoiner, Object> between(char before, char after) Returns an instance that wraps the join result betweenbeforeandafter.between(CharSequence before, CharSequence after) BinaryOperator<com.google.mu.util.stream.Joiner.FasterStringJoiner> combiner()finisher()Joinslandrtogether.join(Collection<?> collection) Joins elements fromcollection.static Joineron(char delimiter) Joining the inputs on thedelimitercharacterstatic Joineron(CharSequence delimiter) Joining the inputs on thedelimiterstringCollector<CharSequence, ?, String> Returns a Collector that skips null and empty string inputs and joins the remaining using this Joiner.Returns a Collector that skips null inputs and joins the remaining using this Joiner.Supplier<com.google.mu.util.stream.Joiner.FasterStringJoiner> supplier()
-
Method Details
-
on
Joining the inputs on thedelimitercharacter -
on
Joining the inputs on thedelimiterstring -
join
-
join
Joins elements fromcollection.joiner.join(list)is equivalent tolist.stream().collect(joiner).- Since:
- 5.7
-
between
Returns an instance that wraps the join result betweenbeforeandafter.For example both
Joiner.on(',').between('[', ']').join(List.of(1, 2))andJoiner.on(',').between('[', ']').join(1, 2)return"[1,2]". -
between
-
skipNulls
-
skipEmpties
Returns a Collector that skips null and empty string inputs and joins the remaining using this Joiner. -
supplier
-
accumulator
- Specified by:
accumulatorin interfaceCollector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
-
combiner
-
finisher
-
characteristics
- Specified by:
characteristicsin interfaceCollector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
-