Java Comma Separated List commaSeparatedList(Object... values)

Here you can find the source of commaSeparatedList(Object... values)

Description

creates a comma separated list out of objects in strings

License

Open Source License

Parameter

Parameter Description
values array of objects

Return

comma separated list

Declaration

public static String commaSeparatedList(Object... values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**//ww  w.  j av a 2 s  .  c  om
     * creates a comma separated list out of objects in strings
     *
     * @param values array of objects
     * @return comma separated list
     */
    public static String commaSeparatedList(Object... values) {
        String toAdd = "";
        for (int i = 0; i < values.length; i++) {
            if (i == values.length - 1) {
                toAdd += stringify(values[i]);
            } else {
                toAdd += stringify(values[i]) + ", ";
            }
        }
        return toAdd;
    }

    /**
     * creates a comma separated list out of objects in string
     *
     * @param values list of objects
     * @return comma separated list
     */
    public static String commaSeparatedList(List<Object> values) {
        return commaSeparatedList(values.toArray(new Object[values.size()]));
    }

    /**
     * Converts a object into a string
     *
     * @param value object to convert
     * @return String value of object
     */
    public static String stringify(Object value) {
        if (value == null) {
            return "";
        } else if (value instanceof Boolean) {
            boolean b = (Boolean) value;
            return (b) ? "true" : "false";
        } else {
            if (value instanceof Integer) {
                return "" + value;
            } else if (value instanceof Double) {
                return "" + value;
            } else if (value instanceof Long) {
                return "" + value;
            } else if (value instanceof Float) {
                return "" + value;
            } else if (value instanceof Enum) {
                return value.toString().toLowerCase();
            } else {
                return value.toString();
            }
        }
    }
}

Related

  1. commalist(List l)
  2. commandToString(List args)
  3. commaSeparate(List a_list)
  4. commaSeparatedClassNames(final List objects)
  5. commaSeparatedList(Collection c)
  6. commaSeparatedList(String string)
  7. commaSeparatedString(List items)
  8. commaSeparatedString(List strings)
  9. commaSeparatedStringToList(String csvListStr)

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