Class Joiner

java.lang.Object
com.google.mu.util.stream.Joiner
All Implemented Interfaces:
Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>

public final class Joiner extends Object implements Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
A joiner (and 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
  • Method Details

    • on

      public static Joiner on(char delimiter)
      Joining the inputs on the delimiter character
    • on

      public static Joiner on(CharSequence delimiter)
      Joining the inputs on the delimiter string
    • join

      public String join(Object l, Object r)
      Joins l and r together.
    • join

      public String join(Collection<?> collection)
      Joins elements from collection.

      joiner.join(list) is equivalent to list.stream().collect(joiner).

      Since:
      5.7
    • between

      public Joiner between(char before, char after)
      Returns an instance that wraps the join result between before and after.

      For example both Joiner.on(',').between('[', ']').join(List.of(1, 2)) and Joiner.on(',').between('[', ']').join(1, 2) return "[1,2]".

    • between

      public Joiner between(CharSequence before, CharSequence after)
    • skipNulls

      public Collector<Object,?,String> skipNulls()
      Returns a Collector that skips null inputs and joins the remaining using this Joiner.
    • skipEmpties

      public Collector<CharSequence, ?, String> skipEmpties()
      Returns a Collector that skips null and empty string inputs and joins the remaining using this Joiner.
    • supplier

      public Supplier<com.google.mu.util.stream.Joiner.FasterStringJoiner> supplier()
      Specified by:
      supplier in interface Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
    • accumulator

      public BiConsumer<com.google.mu.util.stream.Joiner.FasterStringJoiner, Object> accumulator()
      Specified by:
      accumulator in interface Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
    • combiner

      public BinaryOperator<com.google.mu.util.stream.Joiner.FasterStringJoiner> combiner()
      Specified by:
      combiner in interface Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
    • finisher

      public Function<com.google.mu.util.stream.Joiner.FasterStringJoiner, String> finisher()
      Specified by:
      finisher in interface Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>
    • characteristics

      public Set<Collector.Characteristics> characteristics()
      Specified by:
      characteristics in interface Collector<Object, com.google.mu.util.stream.Joiner.FasterStringJoiner, String>