Java Collection Join join(String delimiter, Collection items)

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

Description

Joins string items into one string separated by delimiter.

License

Open Source License

Parameter

Parameter Description
delimiter separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter.
items list with items to be joined

Return

all items joined into one string and separated by delimiter.

Declaration

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

Method Source Code

//package com.java2s;
/*//from  w  w  w.j  av  a  2  s. com
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2015 Maksym Dominichenko
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.*;

public class Main {
    /**
     * Joins string items into one string separated by delimiter.
     *
     * @param delimiter
     *          separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter.
     * @param items
     *          array with items to be joined
     * @return all items joined into one string and separated by delimiter.
     */
    public static String join(String delimiter, String... items) {
        if (isEmpty(items))
            return "";
        StringBuilder result = new StringBuilder();
        boolean delimOk = !isEmpty(delimiter);
        for (Object item : items) {
            if (delimOk && result.length() > 0)
                result.append(delimiter);
            result.append(item);
        }
        return result.toString();
    }

    /**
     * Joins string items into one string separated by delimiter.
     *
     * @param delimiter
     *          separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter.
     * @param items
     *          list with items to be joined
     * @return all items joined into one string and separated by delimiter.
     */
    public static String join(String delimiter, Collection<?> items) {
        if (isEmpty(items))
            return "";
        StringBuilder result = new StringBuilder();
        boolean delimOk = !isEmpty(delimiter);
        for (Object item : items) {
            if (delimOk && result.length() > 0)
                result.append(delimiter);
            result.append(item);
        }
        return result.toString();
    }

    /**
     * Helper to check if the String is {@code null} or empty.<br/>
     * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}.
     *
     * @param string
     *          A string to be checked
     * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise.
     */
    public static boolean isEmpty(String string) {
        return string == null || string.length() == 0;
    }

    /**
     * Helper to check if the array is {@code null} or empty.<br/>
     *
     * @param array
     *          An array to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }

    /**
     * Helper to check if the collection is {@code null} or empty.<br/>
     * {@link Collection#isEmpty()} is not static and therefore require additional check for {@code null}.
     *
     * @param collection
     *          A collection to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Helper to check if the map is {@code null} or empty.<br/>
     * {@link Map#isEmpty()} is not static and therefore require additional check for {@code null}.
     *
     * @param map
     *          A map to be checked
     * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise.
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
}

Related

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