Java Collection Join join(String delimiter, Collection items)

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

Description

Create a string by concatenating the elements of a collection using a delimiter between each item

License

Open Source License

Parameter

Parameter Description
delimiter The text to place between each element in the array
items The collection of items to join

Return

The resulting string

Declaration

public static String join(String delimiter, Collection<String> items) 

Method Source Code

//package com.java2s;
/**/*  ww  w .  j av a  2  s. c o m*/
 * Aptana Studio
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.Collection;

public class Main {
    /**
     * EMPTY
     */
    public static final String EMPTY = "";

    /**
     * Create a string by concatenating the elements of a collection using a delimiter between each item
     * 
     * @param delimiter
     *            The text to place between each element in the array
     * @param items
     *            The collection of items to join
     * @return The resulting string
     */
    public static String join(String delimiter, Collection<String> items) {
        return (items != null) ? join(delimiter, items.toArray(new String[items.size()])) : null;
    }

    /**
     * Create a string by concatenating the elements of a collection using a delimiter between each item
     * 
     * @param delimiter
     *            The text to place between each element in the array
     * @param items
     *            The array of items
     * @return The resulting string
     */
    public static String join(String delimiter, Object... items) {
        String[] s = new String[items.length];
        for (int i = 0; i < items.length; i++) {
            Object item = items[i];
            if (item == null) {
                s[i] = "null"; //$NON-NLS-1$

            } else {
                s[i] = item.toString();
            }
        }
        return join(delimiter, s);
    }

    /**
     * Create a string by concatenating the elements of a collection using a delimiter between each item
     * 
     * @param delimiter
     *            The text to place between each element in the array
     * @param items
     *            The array of chars
     * @return The resulting string
     */
    public static String join(String delimiter, char... items) {
        String[] strings = new String[items.length];
        for (int i = 0; i < items.length; i++) {
            strings[i] = new String(items, i, 1);
        }
        return join(delimiter, strings);
    }

    /**
     * Create a string by concatenating the elements of a string array using a delimiter between each item
     * 
     * @param delimiter
     *            The text to place between each element in the array
     * @param items
     *            The array of items to join
     * @return The resulting string
     */
    public static String join(String delimiter, String... items) {
        String result = null;

        if (items != null) {
            switch (items.length) {
            case 0: {
                result = EMPTY;
                break;
            }

            case 1: {
                result = items[0];
                break;
            }

            // NOTE: consider adding additional cases here, probably for at least 2, by unrolling the loop from the
            // default section below

            default: {
                int lastIndex = items.length - 1;

                // determine length of the delimiter
                int delimiterLength = (delimiter != null) ? delimiter.length() : 0;

                // determine the length of the resulting string, starting with the length of all delimiters
                int targetLength = (lastIndex) * delimiterLength;

                // now add in the length of each item in our list of items
                for (int i = 0; i <= lastIndex; i++) {
                    targetLength += items[i].length();
                }

                // build the resulting character array
                int offset = 0;
                char[] accumulator = new char[targetLength];

                // NOTE: We test for delimiter length here to avoid having a conditional within the for-loops in the
                // true/false blocks. Moving the conditional inside the for-loop barely improved the performance of
                // this implementation from the StringBuilder version we had before
                if (delimiterLength != 0) {
                    // copy all items (except last) and all delimiters
                    for (int i = 0; i < lastIndex; i++) {
                        String item = items[i];

                        // cache current item's length
                        int length = item.length();

                        // copy the item into the accumulator
                        item.getChars(0, length, accumulator, offset);
                        offset += length;

                        // copy in the delimiter
                        delimiter.getChars(0, delimiterLength, accumulator, offset);
                        offset += delimiterLength;
                    }

                    String item = items[lastIndex];
                    item.getChars(0, item.length(), accumulator, offset);
                } else {
                    // NOTE: use classic iteration to avoid the overhead of an iterator
                    for (int i = 0; i <= lastIndex; i++) {
                        String item = items[i];

                        // cache current item's length
                        int length = item.length();

                        // copy the item into the accumulator
                        item.getChars(0, length, accumulator, offset);
                        offset += length;
                    }
                }

                // convert the result to a String and return that value
                result = new String(accumulator);
            }
            }
        }

        return result;
    }
}

Related

  1. join(String delimiter, Collection objects)
  2. join(String delimiter, Collection a)
  3. join(String delimiter, Collection items)
  4. join(String delimiter, Collection objects)
  5. join(String delimiter, Collection parts)
  6. join(String delimiter, Collection stringCollection)
  7. join(String delimiter, Collection strings)
  8. join(String delimiter, Collection values)
  9. join(String glue, Collection c)