Java Collection Join join(Collection elements, String separator)

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

Description

join

License

Open Source License

Declaration

public static String join(Collection<?> elements, String separator) 

Method Source Code

//package com.java2s;
/*//from   w  ww  . j a  va 2 s .c o m
 * This file is part of CraftCommons.
 *
 * Copyright (c) 2011 CraftFire <http://www.craftfire.com/>
 * CraftCommons is licensed under the GNU Lesser General Public License.
 *
 * CraftCommons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CraftCommons 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Object[] elements, String separator) {
        if (elements == null || elements.length <= 0) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < elements.length; ++i) {
            if (i > 0) {
                builder.append(separator);
            }
            if (elements[i] != null) {
                builder.append(elements[i]);
            }
        }
        return builder.toString();
    }

    public static String join(Collection<?> elements, String separator) {
        if (elements == null || elements.isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        Iterator<?> itr = elements.iterator();
        while (itr.hasNext()) {
            builder.append(itr.next());
            if (itr.hasNext()) {
                builder.append(separator);
            }
        }
        return builder.toString();
    }
}

Related

  1. join(Collection collection, String separator)
  2. join(Collection collection, String separator)
  3. join(Collection collection, String separator)
  4. join(Collection collection, String separator)
  5. join(Collection collection, String separatorString)
  6. join(Collection items, char separator)
  7. join(Collection items, String delim)
  8. join(Collection items, String delimiter)
  9. join(Collection iterable, String delimiter)