Java Comma Separated List getStringList(List list, boolean useComma, boolean useBrackets, String comma)

Here you can find the source of getStringList(List list, boolean useComma, boolean useBrackets, String comma)

Description

List the toString out put of the objects in the List that can be comma separated.

License

GNU General Public License

Parameter

Parameter Description
list list of objects with toString methods
useComma true if the list has to be comma separated
useBrackets true if the list has to be enclosed in brackets

Return

comma separated list of the elements in the list

Declaration

public static String getStringList(List<?> list, boolean useComma,
        boolean useBrackets, String comma) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .java2s.c  o  m
 * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
 * <p/>
 * Licensed under the GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.gnu.org/licenses/gpl.html
 * <p/>
 * 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.List;

public class Main {
    /**
     * List the toString out put of the objects in the List comma separated. If
     * the List is null or empty an empty string is returned.
     *
     * The same as getStringList(list, true, false)
     *
     * @see #getStringList(List, boolean, boolean)
     * @param list
     *            list of objects with toString methods
     * @return comma separated list of the elements in the list
     */
    public static String getStringList(List<?> list) {
        return getStringList(list, true, false);
    }

    /**
     * List the toString out put of the objects in the List that can be comma
     * separated. If the List is null or empty an empty string is returned.
     *
     * @param list
     *            list of objects with toString methods
     * @param useComma
     *            true if the list has to be comma separated
     * @param useBrackets
     *            true if the list has to be enclosed in brackets
     * @return comma separated list of the elements in the list
     */
    public static String getStringList(List<?> list, boolean useComma,
            boolean useBrackets, String comma) {
        StringBuffer buffer = new StringBuffer();
        if (!useComma) {
            comma = "";
        }
        if (list != null) {
            if (useBrackets) {
                buffer.append("(");
            }

            for (int i = 0; i < list.size(); i++) {
                buffer.append(list.get(i)).append(
                        (i < list.size() - 1) ? comma : "");
            }

            if (useBrackets) {
                buffer.append(")");
            }
        }

        return buffer.toString();
    }

    public static String getStringList(List<?> list, boolean useComma,
            boolean useBrackets) {
        return getStringList(list, useComma, useBrackets, ",");
    }
}

Related

  1. getCommaSeparatedList(List list)
  2. getCommaSeparatedListOfString( Collection list)
  3. getCommaSeparatedValue(List> enumList)
  4. getCommaSeparatedValues(List values)
  5. getListAsCommaSeparatedString(List list)
  6. getStringListFromCommaSeparatedString(String input)
  7. humanReadableCommandLineOutput(List arguments)
  8. implodeCommaAnd(List list, String comma, String and)
  9. listToComma(List list)