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 betweenbefore
andafter
.between
(CharSequence before, CharSequence after) combiner()
finisher()
Joinsl
andr
together.join
(Collection<?> collection) Joins elements fromcollection
.static Joiner
on
(char delimiter) Joining the inputs on thedelimiter
characterstatic Joiner
on
(CharSequence delimiter) Joining the inputs on thedelimiter
stringReturns 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 thedelimiter
character -
on
Joining the inputs on thedelimiter
string -
join
Joinsl
andr
together. -
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 betweenbefore
andafter
.For example both
Joiner.on(',').between('[', ']').join(List.of(1, 2))
andJoiner.on(',').between('[', ']').join(1, 2)
return"[1,2]"
. -
between
-
skipNulls
Returns a Collector that skips null inputs and joins the remaining using this Joiner. -
skipEmpties
Returns a Collector that skips null and empty string inputs and joins the remaining using this Joiner. -
supplier
- Specified by:
supplier
in interfaceCollector<Object,
StringJoiner, String>
-
accumulator
- Specified by:
accumulator
in interfaceCollector<Object,
StringJoiner, String>
-
combiner
- Specified by:
combiner
in interfaceCollector<Object,
StringJoiner, String>
-
finisher
- Specified by:
finisher
in interfaceCollector<Object,
StringJoiner, String>
-
characteristics
- Specified by:
characteristics
in interfaceCollector<Object,
StringJoiner, String>
-