Class Joiner
- All Implemented Interfaces:
Collector<Object, StringJoiner, 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.
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 TypeMethodDescriptionbetween(char before, char after) Returns an instance that wraps the join result betweenbeforeandafter.between(CharSequence before, CharSequence after) 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()
-
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
- Specified by:
supplierin interfaceCollector<Object, StringJoiner, String>
-
accumulator
- Specified by:
accumulatorin interfaceCollector<Object, StringJoiner, String>
-
combiner
- Specified by:
combinerin interfaceCollector<Object, StringJoiner, String>
-
finisher
- Specified by:
finisherin interfaceCollector<Object, StringJoiner, String>
-
characteristics
- Specified by:
characteristicsin interfaceCollector<Object, StringJoiner, String>
-