Java List Join join(List list, String delimiter)

Here you can find the source of join(List list, String delimiter)

Description

Create a string out of the list's elements using the given delimiter.

License

Apache License

Parameter

Parameter Description
list List to create a string from.
delimiter Delimiter to use between elements.
T Type of elements in the list.

Return

A string created from the given list's elements, delimited by the given delimiter.

Declaration

public static <T> String join(List<T> list, String delimiter) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) 2014 Yevgeny Krasik                                          *
 *                                                                            *
 * 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 {
    /**/* www. j a va  2s .c o  m*/
     * Create a string out of the list's elements using the given delimiter.
     *
     * @param list List to create a string from.
     * @param delimiter Delimiter to use between elements.
     * @param <T> Type of elements in the list.
     * @return A string created from the given list's elements, delimited by the given delimiter.
     */
    public static <T> String join(List<T> list, String delimiter) {
        if (list.isEmpty()) {
            return "";
        }

        final StringBuilder sb = new StringBuilder();
        for (T element : list) {
            sb.append(element);
            sb.append(delimiter);
        }
        sb.delete(sb.length() - delimiter.length(), sb.length());
        return sb.toString();
    }
}

Related

  1. join(List values, String string)
  2. join(List words, String separator)
  3. join(List entries, String separator)
  4. join(List list, String delim)
  5. join(List list, String delim)
  6. join(List list, String joiner)
  7. join(List list, String separator)
  8. join(List objects, String separator)
  9. join(List objs, String delim)