Java Collection Join join(String delim, Collection list)

Here you can find the source of join(String delim, Collection list)

Description

join

License

Open Source License

Declaration

public static String join(String delim, Collection list) 

Method Source Code


//package com.java2s;
/*//ww  w .  j  av  a  2 s. c om
 * CDDL HEADER START
 *
 * 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 usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.util.Collection;

public class Main {
    public static String join(String delim, Object... list) {
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < list.length; i++) {
            if (i != 0 && delim != null) {
                buffer.append(delim);
            }
            if (list[i] != null) {
                buffer.append(list[i]);
            }
        }

        return buffer.toString();
    }

    public static String join(String delim, byte... list) {
        Byte[] array = new Byte[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, char... list) {
        Character[] array = new Character[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, double... list) {
        Double[] array = new Double[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, float... list) {
        Float[] array = new Float[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, int... list) {
        Integer[] array = new Integer[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, long... list) {
        Long[] array = new Long[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, short... list) {
        Short[] array = new Short[list.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = list[i];
        }
        return join(delim, (Object[]) array);
    }

    public static String join(String delim, Collection list) {
        return join(delim, list.toArray());
    }
}

Related

  1. join(final String separator, final Collection objects)
  2. join(final String separator, final Collection objects)
  3. join(Iterable collection, String delimiter)
  4. join(Iterable collection)
  5. join(String c, Collection d)
  6. join(String delim, Collection col)
  7. join(String delim, Collection values)
  8. join(String delim, Collection collections)
  9. join(String delim, Collection values)