Java Collection Join join(Collection collections, String separator)

Here you can find the source of join(Collection collections, String separator)

Description

join

License

Open Source License

Declaration

public static <T> String join(Collection<T> collections, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * SQLPatcher - <a//from www .  j a v  a  2s .c o  m
 * href="https://github.com/kbss/SQLPatcher">https://github.com/kbss
 * /SQLPatcher</a><br>
 * 
 * Copyright (C) 2013 Serhii Krivtsov<br>
 * 
 * SQLPatcher 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 2 of the License, or (at your option) any later
 * version.<br>
 * <br>
 * SQLPatcher 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
 * this program. If not, see <http://www.gnu.org/licenses/>. <br>
 * 
 * @author Serhii Krivtsov
 ******************************************************************************/

import java.util.Collection;

public class Main {
    public static <T> String join(Collection<T> collections, String separator) {
        String result = "";
        if (!collections.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (T object : collections) {
                stringBuilder.append(object.toString()).append(separator);
            }
            int resultLength = stringBuilder.length();
            stringBuilder.delete(resultLength - separator.length(), resultLength);
            result = stringBuilder.toString();
        }
        return result;
    }

    public static <T> String join(Collection<T> collection) {
        return join(collection, ", ");
    }
}

Related

  1. join(Collection c, String concatinator)
  2. join(Collection coll, String separator)
  3. join(Collection collec, String delimiter)
  4. join(Collection collection, CharSequence joinedBy)
  5. join(Collection collection, String joinString)
  6. join(Collection list, final String separator)
  7. join(Collection list, String delimiter)
  8. join(Collection s, String delimiter)
  9. join(Collection things, String delim)