src/share/classes/java/util/StringJoiner.java

Print this page
rev 7597 : 8015318: Extend Collector with 'finish' operation
Reviewed-by:
Contributed-by: brian.goetz@oracle.com


  32  * Prior to adding something to the {@code StringJoiner}, its
  33  * {@code sj.toString()} method will, by default, return {@code prefix + suffix}.
  34  * However, if the {@code setEmptyValue} method is called, the {@code emptyValue}
  35  * supplied will be returned instead. This can be used, for example, when
  36  * creating a string using set notation to indicate an empty set, i.e.
  37  * <code>"{}"</code>, where the {@code prefix} is <code>"{"</code>, the
  38  * {@code suffix} is <code>"}"</code> and nothing has been added to the
  39  * {@code StringJoiner}.
  40  *
  41  * @apiNote
  42  * <p>The String {@code "[George:Sally:Fred]"} may be constructed as follows:
  43  *
  44  * <pre> {@code
  45  * StringJoiner sj = new StringJoiner(":", "[", "]");
  46  * sj.add("George").add("Sally").add("Fred");
  47  * String desiredString = sj.toString();
  48  * }</pre>
  49  * <p>
  50  * A {@code StringJoiner} may be employed to create formatted output from a
  51  * {@link java.util.stream.Stream} using
  52  * {@link java.util.stream.Collectors#toStringJoiner}. For example:
  53  *
  54  * <pre> {@code
  55  * List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
  56  * String commaSeparatedNumbers = numbers.stream()
  57  *     .map(i -> i.toString())
  58  *     .collect(Collectors.toStringJoiner(", ")).toString();
  59  * }</pre>
  60  *
  61  * @see java.util.stream.Collectors#toStringJoiner
  62  * @since  1.8
  63 */
  64 public final class StringJoiner {
  65     private final String prefix;
  66     private final String delimiter;
  67     private final String suffix;
  68 
  69     /*
  70      * StringBuilder value -- at any time, the characters constructed from the
  71      * prefix, the added element separated by the delimiter, but without the
  72      * suffix, so that we can more easily add elements without having to jigger
  73      * the suffix each time.
  74      */
  75     private StringBuilder value;
  76 
  77     /*
  78      * By default, the string consisting of prefix+suffix, returned by
  79      * toString(), or properties of value, when no elements have yet been added,
  80      * i.e. when it is empty.  This may be overridden by the user to be some
  81      * other value including the empty String.




  32  * Prior to adding something to the {@code StringJoiner}, its
  33  * {@code sj.toString()} method will, by default, return {@code prefix + suffix}.
  34  * However, if the {@code setEmptyValue} method is called, the {@code emptyValue}
  35  * supplied will be returned instead. This can be used, for example, when
  36  * creating a string using set notation to indicate an empty set, i.e.
  37  * <code>"{}"</code>, where the {@code prefix} is <code>"{"</code>, the
  38  * {@code suffix} is <code>"}"</code> and nothing has been added to the
  39  * {@code StringJoiner}.
  40  *
  41  * @apiNote
  42  * <p>The String {@code "[George:Sally:Fred]"} may be constructed as follows:
  43  *
  44  * <pre> {@code
  45  * StringJoiner sj = new StringJoiner(":", "[", "]");
  46  * sj.add("George").add("Sally").add("Fred");
  47  * String desiredString = sj.toString();
  48  * }</pre>
  49  * <p>
  50  * A {@code StringJoiner} may be employed to create formatted output from a
  51  * {@link java.util.stream.Stream} using
  52  * {@link java.util.stream.Collectors#joining}. For example:
  53  *
  54  * <pre> {@code
  55  * List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
  56  * String commaSeparatedNumbers = numbers.stream()
  57  *     .map(i -> i.toString())
  58  *     .collect(Collectors.joining(", "));
  59  * }</pre>
  60  *
  61  * @see java.util.stream.Collectors#joining
  62  * @since  1.8
  63 */
  64 public final class StringJoiner {
  65     private final String prefix;
  66     private final String delimiter;
  67     private final String suffix;
  68 
  69     /*
  70      * StringBuilder value -- at any time, the characters constructed from the
  71      * prefix, the added element separated by the delimiter, but without the
  72      * suffix, so that we can more easily add elements without having to jigger
  73      * the suffix each time.
  74      */
  75     private StringBuilder value;
  76 
  77     /*
  78      * By default, the string consisting of prefix+suffix, returned by
  79      * toString(), or properties of value, when no elements have yet been added,
  80      * i.e. when it is empty.  This may be overridden by the user to be some
  81      * other value including the empty String.