Java Collection Join join(Collection objs, String delimiter)

Here you can find the source of join(Collection objs, String delimiter)

Description

Converts an array of objects to a delimited string representation.

License

Open Source License

Parameter

Parameter Description
objs the array of items to join
delimiter the text to place between each element in the array, cannot be <code>null</code>. To join without a delimiter, use an empty string.

Exception

Parameter Description
NullPointerException if the received delimiter is null.

Return

the resulting string on null if items is null

Declaration

public static String join(Collection<?> objs, String delimiter) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/*w  ww .j  a  v  a2 s.com*/
     * Converts an array of objects to a delimited string representation.
     * <p>
     * <<<<<<< HEAD The stirng representation of each element is obtained via
     * <code>toString()</code>. If a element of the array is <code>null</code> it is
     * skiped. ======= The string representation of each element is obtained via
     * <code>toString()</code>. If a element of the array is <code>null</code> it is
     * skipped. >>>>>>> [FIX] Corrects an off-by-one bug on StringUtil.join()
     * 
     * @param objs the array of items to join
     * @param delimiter the text to place between each element in the array, cannot be
     *            <code>null</code>. To join without a delimiter, use an empty string.
     * @return the resulting string on <code>null</code> if items is <code>null</code>
     * @throws NullPointerException if the received delimiter is null.
     */
    public static String join(Collection<?> objs, String delimiter) {
        assert delimiter != null : "Delimiter should not be null";

        if (objs == null || objs.size() == 0) {
            return "";
        }

        StringBuffer buffer = new StringBuffer();
        for (Object obj : objs) {
            if (buffer.length() > 0) {
                buffer.append(delimiter);
            }
            buffer.append(obj);
        }
        return buffer.toString();
    }

    /**
     * Converts an array of integer objects to a delimited string representation.
     * <p>
     * 
     * @param ints the array of items to join
     * @param delimiter the text to place between each element in the array; cannot be
     *            <code>null</code>. To join without a delimiter, use an empty string.
     * @return the resulting string or <code>null</code> if items is <code>null</code>
     */
    public static String join(int[] ints, String delimiter) {
        assert delimiter != null : "Delimiter should not be null";

        if (ints == null || ints.length == 0) {
            return "";
        }

        final StringBuffer buffer = new StringBuffer();
        for (int obj : ints) {
            if (buffer.length() > 0) {
                buffer.append(delimiter);
            }
            buffer.append(Integer.toString(obj));
        }
        return buffer.toString();
    }

    /**
     * Converts an array of objects to a delimited string representation.
     * <p>
     * The string representation of each element is obtained via <code>toString()</code>.
     * If a element of the array is <code>null</code> it is skipped.
     * 
     * @param objs the array of items to join
     * @param delimiter the text to place between each element in the array, cannot be
     *            <code>null</code>. To join without a delimiter, use an empty string.
     * @return the resulting string or <code>null</code> if items is <code>null</code>
     */
    public static String join(final Object[] objs, final String delimiter) {
        assert delimiter != null : "Delimiter should not be null";

        if (objs == null) {
            return null;
        }

        final StringBuffer sb = new StringBuffer();

        for (int i = 0; i < objs.length; i++) {
            final Object o = objs[i];
            if (o != null) {
                if (sb.length() > 0) {
                    sb.append(delimiter);
                }

                sb.append(o);
            }
        }

        return sb.toString();
    }

    /**
     * Converts an array of shorts to a delimited string representation.
     * <p>
     * 
     * @param shorts the array of items to join
     * @param delimiter the text to place between each element in the array; cannot be
     *            <code>null</code>. To join without a delimiter, use an empty string.
     * @return the resulting string or <code>null</code> if items is <code>null</code>
     */
    public static String join(short[] shorts, String delimiter) {
        assert delimiter != null : "Delimiter should not be null";

        if (shorts == null || shorts.length == 0) {
            return "";
        }

        StringBuffer buffer = new StringBuffer();
        for (int obj : shorts) {
            if (buffer.length() > 0) {
                buffer.append(delimiter);
            }
            buffer.append(Integer.toString(obj));
        }
        return buffer.toString();
    }
}

Related

  1. join(Collection iterable, String delimiter)
  2. join(Collection objects, String token, StringBuilder buf)
  3. join(Collection objectsToJoin, String separator)
  4. join(Collection objs, String delim)
  5. join(Collection objs, String delim)
  6. join(Collection objs, String sep)
  7. join(Collection s)
  8. join(Collection s, String delimiter)
  9. join(Collection s, String delimiter)