Java Collection to String collectionToString(Collection collection)

Here you can find the source of collectionToString(Collection collection)

Description

Returns a collection of Strings as a comma-delimitted list of strings.

License

Open Source License

Return

a String representing the Collection.

Declaration

public static String collectionToString(Collection<String> collection) 

Method Source Code

//package com.java2s;
/**/*from  ww  w.java 2 s . c  o  m*/
 * Check whether the given String has actual text. More specifically,
 * returns <code>true</code> if the string not <code>null</code>, its length
 * is greater than 0, and it contains at least one non-whitespace character.
 * <p/>
 * <code>StringUtils.hasText(null) == false<br/>
 * StringUtils.hasText("") == false<br/>
 * StringUtils.hasText(" ") == false<br/>
 * StringUtils.hasText("12345") == true<br/>
 * StringUtils.hasText(" 12345 ") == true</code>
 * <p/>
 * <p>
 * Copied from the Spring Framework while retaining all license, copyright
 * and author information.
 *
 * @param str the String to check (may be <code>null</code>)
 * @return <code>true</code> if the String is not <code>null</code>, its
 * length is greater than 0, and it does not contain whitespace only
 * @see Character#isWhitespace
 */

import java.util.Collection;

public class Main {
    /**
     * Constant representing the empty string, equal to &quot;&quot;
     */
    public static final String EMPTY_STRING = "";

    /**
     * Returns a collection of Strings as a comma-delimitted list of strings.
     *
     * @return a String representing the Collection.
     */
    public static String collectionToString(Collection<String> collection) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }
        StringBuilder buf = new StringBuilder();
        String delim = "";
        for (String element : collection) {
            buf.append(delim);
            buf.append(element);
            delim = ",";
        }
        return buf.toString();
    }

    public static boolean isEmpty(String s) {
        return isBlank(s);
    }

    /**
     * Returns the specified array as a comma-delimited (',') string.
     *
     * @param array the array whose contents will be converted to a string.
     * @return the array's contents as a comma-delimited (',') string.
     * @since 1.0
     */
    public static String toString(Object[] array) {
        return toDelimitedString(array, ",");
    }

    public static boolean isBlank(String s) {
        if (s == null) {
            return true;
        }
        if (s.trim().length() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Returns the array's contents as a string, with each element delimited by
     * the specified {@code delimiter} argument. Useful for {@code toString()}
     * implementations and log messages.
     *
     * @param array the array whose contents will be converted to a string
     * @param delimiter the delimiter to use between each element
     * @return a single string, delimited by the specified {@code delimiter}.
     * @since 1.0
     */
    public static String toDelimitedString(Object[] array, String delimiter) {
        if (array == null || array.length == 0) {
            return EMPTY_STRING;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                sb.append(delimiter);
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. collectionToString(Collection elements)
  2. collectionToString(Collection list)
  3. collectionToString(Collection list)
  4. collectionToString(Collection collection, String prefix)
  5. collectionToString(Collection collection)
  6. collectionToString(Collection collection)
  7. collectionToString(Collection collection)
  8. collectionToString(Collection col, String sep)
  9. collectionToString(Collection collection)

  10. HOME | Copyright © www.java2s.com 2016