Java Collection Join joinCommaDelimitedList(Collection list)

Here you can find the source of joinCommaDelimitedList(Collection list)

Description

Convert a Collection into a comma delimited String (i.e., CSV).

License

Apache License

Parameter

Parameter Description
list the collection

Return

the delimited String

Declaration

public static String joinCommaDelimitedList(Collection<?> list) 

Method Source Code

//package com.java2s;
/**/*  ww  w  .  j ava  2  s. com*/
 * Copyright 2008-2016 Juho Jeong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    /** The empty {@link String} */
    public static final String EMPTY = "";

    /**
     * Convert a {@code String} array into a comma delimited {@code String}
     * (i.e., CSV).
     * 
     * @param arr the array to display
     * @return the delimited {@code String}
     */
    public static String joinCommaDelimitedList(String[] arr) {
        return arrayToDelimitedString(arr, ", ");
    }

    /**
     * Convert a {@code Collection} into a comma delimited {@code String}
     * (i.e., CSV).
     * 
     * @param list the collection
     * @return the delimited {@code String}
     */
    public static String joinCommaDelimitedList(Collection<?> list) {
        if (list == null || list.isEmpty()) {
            return EMPTY;
        }
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Object o : list) {
            if (!first) {
                sb.append(", ");
            }
            sb.append(o);
            first = false;
        }
        return sb.toString();
    }

    /**
     * Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     * 
     * @param arr the array to display
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String arrayToDelimitedString(Object[] arr, String delim) {
        if (arr == null || arr.length == 0) {
            return EMPTY;
        }
        if (arr.length == 1) {
            return (arr[0] == null) ? EMPTY : arr[0].toString();
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * Returns {@code true} if the given string is null or is the empty string.
     *
     * @param str a string reference to check
     * @return {@code true} if the string is null or is the empty string
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related

  1. joinCol(Collection lst, String delim)
  2. joinCollection(Collection collection, char delimiter)
  3. joinCollection(Collection a)
  4. joinCollections(final Collection target, final Collection... collections)
  5. joinCollectionToString(Collection list, String str)
  6. joinEmptyItemIncluded(Collection stringCollection, String delimiter)
  7. joinList(@SuppressWarnings("rawtypes") Collection c)
  8. joinNullSafe(Collection collection, String separator)
  9. joinObjects(Collection objects)

    HOME | Copyright © www.java2s.com 2016