Java List Join joinList(final List list)

Here you can find the source of joinList(final List list)

Description

Joins the elements using comma as a separator.

License

Apache License

Declaration

public final static String joinList(final List<String> list) 

Method Source Code


//package com.java2s;
/*/*from ww w.j  a  v a  2s  .c o  m*/
 * Copyright 2008 ETH Zuerich, CISD
 *
 * 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.List;

public class Main {
    public final static String EMPTY = "";

    /**
     * Joins the elements using comma as a separator. <br>
     * Example: "a", "b", "c" -> "a, b, c"
     */
    public final static String joinList(final List<String> list) {
        if (list == null) {
            return null;
        }
        return join(list.toArray(new String[0]), ",");
    }

    /**
     * Joins the elements of the provided array into a single <code>String</code> containing the
     * provided list of elements.
     */
    public final static String join(final Object[] array, final String separator) {
        if (array == null) {
            return null;
        }
        final String sep = separator == null ? EMPTY : separator;
        final StringBuilder builder = new StringBuilder();
        boolean start = true;
        for (final Object element : array) {
            if (start == false) {
                builder.append(sep);
            }
            builder.append(element);
            start = false;
        }
        return builder.toString();
    }
}

Related

  1. joinCommaSeparatedList(List list)
  2. joinDoubles(List list)
  3. joinIntegers(List integers, String token)
  4. joinLines(List lines)
  5. joinLinesWithImplicitFinalLine(final List lines)
  6. joinList(final List list, final String sep)
  7. joinList(List list, String separator)
  8. joinList(List list, String separator)
  9. joinList(List items)