Java Collection Join join(final Collection values, final char separator)

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

Description

Joins a collection of header values into a single header value, with a specified specified separator.

License

CDDL license

Parameter

Parameter Description
values the values to be joined.
separator the separator to separate values within the returned value.

Return

a single header value, with values separated by the separator.

Declaration

public static String join(final Collection<String> values, final char separator) 

Method Source Code

//package com.java2s;
/*/*from   w  ww  .  ja v  a  2  s  .  com*/
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2010?2011 ApexIdentity Inc.
 * Portions Copyright 2011-2014 ForgeRock AS.
 */

import java.util.Collection;

public class Main {
    /**
     * Joins a collection of header values into a single header value, with a specified
     * specified separator. A {@code null} or empty collection of header values yeilds a
     * {@code null} return value.
     *
     * @param values the values to be joined.
     * @param separator the separator to separate values within the returned value.
     * @return a single header value, with values separated by the separator.
     */
    public static String join(final Collection<String> values, final char separator) {
        if (separator == '"' || separator == '\\') {
            throw new IllegalArgumentException("invalid separator: " + separator);
        }
        final StringBuilder sb = new StringBuilder();
        if (values != null) {
            for (final String s : values) {
                if (s != null) {
                    if (sb.length() > 0) {
                        sb.append(separator).append(' ');
                    }
                    sb.append(s);
                }
            }
        }
        return sb.length() > 0 ? sb.toString() : null;
    }
}

Related

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