Top Description Inners Fields Constructors Methods
com.google.common.base

public Class Joiner

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, java.util.Objects.requireNonNull

An object which joins pieces of text (specified as an array, Iterable, varargs or even a Map) with a separator. It either appends the results to an Appendable or returns them as a String. Example:
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.

// 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.

Author
Kevin Bourrillion
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Joiner.MapJoiner

An object that joins map entries in the same manner as Joiner joins iterables and arrays.

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
private
Joiner(String separator)

private
Joiner(Joiner prototype)

Method Summary

Modifier and TypeMethod and Description
public <A extends Appendable> A
appendTo(A appendable, Iterable<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

public <A extends Appendable> A
appendTo(A appendable, Iterator<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

public final <A extends Appendable> A
appendTo(A appendable, @Nullable Object[] parts)

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

public final <A extends Appendable> A
appendTo(A appendable, Object first, Object second, @Nullable Object... rest)

Appends to appendable the string representation of each of the remaining arguments.

public final StringBuilder
appendTo(StringBuilder builder, Iterable<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder.

public final StringBuilder
appendTo(StringBuilder builder, Iterator<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder.

public final StringBuilder
appendTo(StringBuilder builder, @Nullable Object[] parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder.

public final StringBuilder
appendTo(StringBuilder builder, Object first, Object second, @Nullable Object... rest)

Appends to builder the string representation of each of the remaining arguments.

private static Iterable<@Nullable Object>
iterable(Object first, Object second, @Nullable Object[] rest)

public final String
join(Iterable<? extends @Nullable Object> parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

public final String
join(Iterator<? extends @Nullable Object> parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

public final String
join(@Nullable Object[] parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

public final String
join(Object first, Object second, @Nullable Object... rest)

Returns a string containing the string representation of each argument, using the previously configured separator between each.

public static Joiner
on(String separator)

Returns a joiner which automatically places separator between consecutive elements.

public static Joiner
on(char separator)

Returns a joiner which automatically places separator between consecutive elements.

public Joiner
skipNulls()

Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.

pack-priv CharSequence
public Joiner
useForNull(String nullText)

Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.

public Joiner.MapJoiner
withKeyValueSeparator(char keyValueSeparator)

Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.

public Joiner.MapJoiner
withKeyValueSeparator(String keyValueSeparator)

Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

separatorback to summary
private final String separator

Constructor Detail

Joinerback to summary
private Joiner(String separator)
Joinerback to summary
private Joiner(Joiner prototype)

Method Detail

appendToback to summary
public <A extends Appendable> A appendTo(A appendable, Iterable<? extends @Nullable Object> parts) throws IOException

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public <A extends Appendable> A appendTo(A appendable, Iterator<? extends @Nullable Object> parts) throws IOException

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

Annotations
@CanIgnoreReturnValue
Since
11.0
appendToback to summary
public final <A extends Appendable> A appendTo(A appendable, @Nullable Object[] parts) throws IOException

Appends the string representation of each of parts, using the previously configured separator between each, to appendable.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public final <A extends Appendable> A appendTo(A appendable, Object first, Object second, @Nullable Object... rest) throws IOException

Appends to appendable the string representation of each of the remaining arguments.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public final StringBuilder appendTo(StringBuilder builder, Iterable<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public final StringBuilder appendTo(StringBuilder builder, Iterator<? extends @Nullable Object> parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
Since
11.0
appendToback to summary
public final StringBuilder appendTo(StringBuilder builder, @Nullable Object[] parts)

Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public final StringBuilder appendTo(StringBuilder builder, Object first, Object second, @Nullable Object... rest)

Appends to builder the string representation of each of the remaining arguments. Identical to appendTo(Appendable, Object, Object, Object...), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
iterableback to summary
private static Iterable<@Nullable Object> iterable(Object first, Object second, @Nullable Object[] rest)
joinback to summary
public final String join(Iterable<? extends @Nullable Object> parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

joinback to summary
public final String join(Iterator<? extends @Nullable Object> parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

Since
11.0
joinback to summary
public final String join(@Nullable Object[] parts)

Returns a string containing the string representation of each of parts, using the previously configured separator between each.

joinback to summary
public final String join(Object first, Object second, @Nullable Object... rest)

Returns a string containing the string representation of each argument, using the previously configured separator between each.

onback to summary
public static Joiner on(String separator)

Returns a joiner which automatically places separator between consecutive elements.

onback to summary
public static Joiner on(char separator)

Returns a joiner which automatically places separator between consecutive elements.

skipNullsback to summary
public Joiner skipNulls()

Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.

toStringback to summary
pack-priv CharSequence toString(Object part)
useForNullback to summary
public Joiner useForNull(String nullText)

Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.

withKeyValueSeparatorback to summary
public Joiner.MapJoiner withKeyValueSeparator(char keyValueSeparator)

Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.

Since
20.0
withKeyValueSeparatorback to summary
public Joiner.MapJoiner withKeyValueSeparator(String keyValueSeparator)

Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.

com.google.common.base back to summary

public final Class Joiner.MapJoiner

extends Object
Class Inheritance

An object that joins map entries in the same manner as Joiner joins iterables and arrays. Like Joiner, it is thread-safe and immutable.

In addition to operating on Map instances, MapJoiner can operate on Multimap entries in two distinct modes:

Since
2.0

Field Summary

Modifier and TypeField and Description
private final Joiner
private final String

Constructor Summary

AccessConstructor and Description
private
MapJoiner(Joiner joiner, String keyValueSeparator)

Method Summary

Modifier and TypeMethod and Description
public <A extends Appendable> A
appendTo(A appendable, Map<?, ?> map)

Appends the string representation of each entry of map, using the previously configured separator and key-value separator, to appendable.

public StringBuilder
appendTo(StringBuilder builder, Map<?, ?> map)

Appends the string representation of each entry of map, using the previously configured separator and key-value separator, to builder.

public <A extends Appendable> A
appendTo(A appendable, Iterable<? extends Map.Entry<?, ?>> entries)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to appendable.

public <A extends Appendable> A
appendTo(A appendable, Iterator<? extends Map.Entry<?, ?>> parts)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to appendable.

public StringBuilder
appendTo(StringBuilder builder, Iterable<? extends Map.Entry<?, ?>> entries)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to builder.

public StringBuilder
appendTo(StringBuilder builder, Iterator<? extends Map.Entry<?, ?>> entries)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to builder.

public String
join(Map<?, ?> map)

Returns a string containing the string representation of each entry of map, using the previously configured separator and key-value separator.

public String
join(Iterable<? extends Map.Entry<?, ?>> entries)

Returns a string containing the string representation of each entry in entries, using the previously configured separator and key-value separator.

public String
join(Iterator<? extends Map.Entry<?, ?>> entries)

Returns a string containing the string representation of each entry in entries, using the previously configured separator and key-value separator.

public Joiner.MapJoiner
useForNull(String nullText)

Returns a map joiner with the same behavior as this one, except automatically substituting nullText for any provided null keys or values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

joinerback to summary
private final Joiner joiner
keyValueSeparatorback to summary
private final String keyValueSeparator

Constructor Detail

MapJoinerback to summary
private MapJoiner(Joiner joiner, String keyValueSeparator)

Method Detail

appendToback to summary
public <A extends Appendable> A appendTo(A appendable, Map<?, ?> map) throws IOException

Appends the string representation of each entry of map, using the previously configured separator and key-value separator, to appendable.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public StringBuilder appendTo(StringBuilder builder, Map<?, ?> map)

Appends the string representation of each entry of map, using the previously configured separator and key-value separator, to builder. Identical to appendTo(Appendable, Map), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
appendToback to summary
public <A extends Appendable> A appendTo(A appendable, Iterable<? extends Map.Entry<?, ?>> entries) throws IOException

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to appendable.

Annotations
@CanIgnoreReturnValue
Since
10.0
appendToback to summary
public <A extends Appendable> A appendTo(A appendable, Iterator<? extends Map.Entry<?, ?>> parts) throws IOException

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to appendable.

Annotations
@CanIgnoreReturnValue
Since
11.0
appendToback to summary
public StringBuilder appendTo(StringBuilder builder, Iterable<? extends Map.Entry<?, ?>> entries)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
Since
10.0
appendToback to summary
public StringBuilder appendTo(StringBuilder builder, Iterator<? extends Map.Entry<?, ?>> entries)

Appends the string representation of each entry in entries, using the previously configured separator and key-value separator, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw IOException.

Annotations
@CanIgnoreReturnValue
Since
11.0
joinback to summary
public String join(Map<?, ?> map)

Returns a string containing the string representation of each entry of map, using the previously configured separator and key-value separator.

joinback to summary
public String join(Iterable<? extends Map.Entry<?, ?>> entries)

Returns a string containing the string representation of each entry in entries, using the previously configured separator and key-value separator.

Since
10.0
joinback to summary
public String join(Iterator<? extends Map.Entry<?, ?>> entries)

Returns a string containing the string representation of each entry in entries, using the previously configured separator and key-value separator.

Since
11.0
useForNullback to summary
public Joiner.MapJoiner useForNull(String nullText)

Returns a map joiner with the same behavior as this one, except automatically substituting nullText for any provided null keys or values.