Java Collection Join join(final Collection values)

Here you can find the source of join(final Collection values)

Description

Joins a collection of values with commas, enclosed by brackets.

License

Apache License

Declaration

static String join(final Collection<String> values) 

Method Source Code

//package com.java2s;
/*//ww w . j av  a2 s  .c  o m
 * Copyright 2011-2012 Amazon Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *    http://aws.amazon.com/apache2.0
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    /**
     * Joins a collection of values with commas, enclosed by brackets. An empty
     * or null set of values returns the empty string.
     */
    static String join(final Collection<String> values) {
        return join(values, false);
    }

    /**
     * Joins a collection of values with commas, enclosed by brackets. An empty
     * or null set of values returns the empty string.
     *
     * @param quoted
     *            Whether each value should be quoted in the output.
     */
    static String join(final Collection<String> values, boolean quoted) {
        if (values == null || values.isEmpty()) {
            return "";
        }

        StringBuilder builder = new StringBuilder("{");
        boolean seenOne = false;
        for (String s : values) {
            if (seenOne) {
                builder.append(",");
            } else {
                seenOne = true;
            }
            builder.append(quoted ? "\"" + s + "\"" : s);
        }
        builder.append("}");

        return builder.toString();
    }
}

Related

  1. join(final Collection collection, final String separator)
  2. join(final Collection list, final String separator)
  3. join(final Collection seq, final String delimiter)
  4. join(final Collection strs, final String delimiter)
  5. join(final Collection toJoin)
  6. join(final Collection values, final char separator)
  7. join(final Collection collection)
  8. join(final Collection collection, final String delimiter)
  9. join(final Collection objs, final String delimiter)