public class Joiner extends Object
Iterable
, varargs or even a
Map
) with a separator. It either appends the results to an Appendable
or returns
them as a String
. Example:
<p>
Joiner joiner = Joiner.on("; ").skipNulls();
. . .
return joiner.join("Harry", null, "Ron", "Hermione");
This returns the string "Harry; Ron; Hermione"
. Note that all input elements are
converted to strings using Object.toString()
before being appended.
If neither #skipNulls()
nor #useForNull(String)
is specified, the joining
methods will throw NullPointerException
if any given element is null.
Warning: joiner instances are always immutable; a configuration method such as useForNull
has no effect on the instance it is invoked on! You must store and use the new joiner
instance returned by the method. This makes joiners thread-safe, and safe to store as static final
constants.
<p>
// Bad! Do not do this!
Joiner joiner = Joiner.on(',');
joiner.skipNulls(); // does nothing!
return joiner.join("wrong", null, "wrong");
See the Guava User Guide article on Joiner
.
Modifier and Type | Class and Description |
---|---|
static class |
Joiner.MapJoiner
An object that joins map entries in the same manner as
Joiner joins iterables and
arrays. |
Modifier and Type | Method and Description |
---|---|
static Joiner |
on()
Returns a joiner which automatically places
separator between consecutive elements. |
Joiner.MapJoiner |
withKeyValueSeparator()
Returns a
MapJoiner using the given key-value separator, and the same configuration as
this Joiner otherwise. |
public static Joiner on()
separator
between consecutive elements.public Joiner.MapJoiner withKeyValueSeparator()
MapJoiner
using the given key-value separator, and the same configuration as
this Joiner
otherwise.Copyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.