Java Collection Join join(Collection values)

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

Description

Joins a collection of objects' toString() values into a single string, using the default delimiter of ", " (comma-space).

License

Open Source License

Parameter

Parameter Description
values the values

Return

the delimiter-separated string

Declaration

public static String join(Collection<?> values) 

Method Source Code

//package com.java2s;
/*// w  w w.  j  a  va 2  s . com
 * Copyright (c) 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.Collection;

public class Main {
    /** Default delimiter for the {@link #join} methods. */
    public static final String DEFAULT_JOIN_DELIMITER = ", ";

    /**
     * Joins a collection of objects' toString() values into a single string,
     * using the default delimiter of ", " (comma-space).
     *
     * @param values the values
     * @return the delimiter-separated string
     */
    public static String join(Collection<?> values) {
        if (values == null)
            return null;
        return join(values.toArray(new Object[values.size()]));
    }

    /**
     * Joins an array of objects' toString() values into a single string,
     * using the default delimiter of ", " (comma-space).
     *
     * @param values the values
     * @return the delimiter-separated string
     */
    public static String join(Object[] values) {
        return join(values, DEFAULT_JOIN_DELIMITER);
    }

    /**
     * Joins a collection of objects' toString() values into a single string,
     * using the given delimiter. If the collection is null, null is returned.
     *
     * @param values the values
     * @param delim the delimiter
     * @return the delimiter-separated string
     */
    public static String join(Collection<?> values, String delim) {
        if (values == null)
            return null;
        return join(values.toArray(new Object[values.size()]), delim);
    }

    /**
     * Joins an array of objects' toString() values into a single string,
     * using the given delimiter. If the array is null, null is returned.
     *
     * @param values the values
     * @param delim the delimiter
     * @return the delimiter-separated string
     */
    public static String join(Object[] values, String delim) {
        if (values == null)
            return null;

        StringBuilder sb = new StringBuilder();
        for (Object v : values) {
            if (sb.length() > 0)
                sb.append(delim);
            sb.append(v);
        }
        return sb.toString();
    }
}

Related

  1. join(Collection strings, String separator)
  2. join(Collection strings, String separator)
  3. join(Collection strs)
  4. join(Collection toJoin, String joinBy)
  5. join(Collection tokens, String d)
  6. join(Collection collection, String separator)
  7. join(Collection data)
  8. join(Collection c, String separator)
  9. join(Collection args, String delimeter)