Java Collection Join join(Collection list, String delimiter)

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

Description

Joins the list members into a delimiter separated String.

License

Open Source License

Parameter

Parameter Description
list a parameter
delimiter a parameter

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w w w  .  jav a 2 s .c o m
 * This file is part of ConfigHub.
 *
 * ConfigHub is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ConfigHub is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.??See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ConfigHub.??If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    /**
     * Joins the list members into a delimiter separated String.
     *
     * @param list
     * @param delimiter
     * @return
     */
    public static <T> String join(Collection<T> list, String delimiter) {
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (T line : list) {
            sb.append(line);
            if (list.size() != index + 1) {
                sb.append(delimiter);
            }
            index++;
        }

        return sb.toString();
    }

    public static <T> String join(T[] list, String delimiter) {
        if (null == list) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (int i = 0; i < list.length; i++) {
            String line = list[i].toString();
            sb.append(line.trim());
            if (list.length != index + 1) {
                sb.append(delimiter);
            }
            index++;
        }

        return sb.toString();
    }
}

Related

  1. join(Collection collec, String delimiter)
  2. join(Collection collection, CharSequence joinedBy)
  3. join(Collection collection, String joinString)
  4. join(Collection collections, String separator)
  5. join(Collection list, final String separator)
  6. join(Collection s, String delimiter)
  7. join(Collection things, String delim)
  8. join(Collection values, String join)
  9. join(final char _separator, final boolean _quotes, final Collection _list, final String _emptyString)