Java Collection Create createCsvString(Collection in)

Here you can find the source of createCsvString(Collection in)

Description

Create string with comma separated list of values from input collection.

License

Open Source License

Parameter

Parameter Description
in collection to format

Return

null if in is null, CSV string in other cases (empty id in is empty)

Declaration

public static String createCsvString(Collection<String> in) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from  www .ja v a  2s.c  o  m*/
     * Create string with comma separated list of values from input collection. Ordering by used Collection implementation
     * iteration order is used.
     * 
     * @param in collection to format
     * @return <code>null</code> if <code>in</code> is <code>null</code>, CSV string in other cases (empty id in is empty)
     */
    public static String createCsvString(Collection<String> in) {
        if (in == null)
            return null;
        if (in.isEmpty()) {
            return "";
        }
        boolean first = true;
        StringBuilder sb = new StringBuilder();
        for (String s : in) {
            if (first)
                first = false;
            else
                sb.append(",");
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * Check if String value is null or empty.
     * 
     * @param src value
     * @return <code>true</code> if value is null or empty
     */
    public static boolean isEmpty(String src) {
        return (src == null || src.length() == 0 || src.trim().length() == 0);
    }
}

Related

  1. createCollection(Object[] objects)
  2. createCollection(T... items)
  3. createCollection(X o)
  4. createCollectionFromString(String value)
  5. createCSV(Collection list)
  6. createFloatArray(java.util.Collection collection)
  7. createInstance( Class collectionType)
  8. createIntArray(Collection collection)
  9. createIntegerInClause(Collection p_integers)