Java List Join join(String separator, Collection inputList)

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

Description

Join the strings contained in inputList with the separator.

License

Open Source License

Parameter

Parameter Description
separator The String to glue the strings in inputList together with
inputList The List of Strings to join

Return

The joined String

Declaration

public static String join(String separator, Collection<String> inputList) 

Method Source Code

//package com.java2s;
/**//w  ww .  j a  v  a2s.c  o m
 * Copyright (c) 2009--2017 Red Hat, Inc.
 *
 * This software is licensed to you under the GNU General Public License,
 * version 2 (GPLv2). There is NO WARRANTY for this software, express or
 * implied, including the implied warranties of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 * along with this software; if not, see
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * Red Hat trademarks are not licensed under GPLv2. No permission is
 * granted to use or replicate Red Hat trademarks that are incorporated
 * in this software or its documentation.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Join the strings contained in inputList with the separator. Returns null
     * if the input list is empty
     *
     * @param separator The String to glue the strings in inputList together
     * with
     * @param inputList The List of Strings to join
     * @return The joined String
     */
    public static String join(String separator, Collection<String> inputList) {
        Iterator<String> itty = inputList.iterator();

        return join(separator, itty);
    }

    /**
     * Join the strings contained in an iterator with a separator. Used by
     * join(String,List) and acts as a convenience for the few times we want to
     * use join without a list.
     * @param separator The String separator, use
     * <code>Localization.getInstance().getMessage("list delimiter")</code> for
     * the appropriate display separator.
     * @param itty The iterator containing display items.
     * @return The joined String
     */
    public static String join(String separator, Iterator<String> itty) {
        if (!itty.hasNext()) {
            return null;
        }

        StringBuilder ret = new StringBuilder();
        ret.append(itty.next());

        while (itty.hasNext()) {
            ret.append(separator);
            ret.append(itty.next());
        }

        return ret.toString();
    }
}

Related

  1. join(String joiner, List joinees)
  2. join(String joinString, List strings)
  3. join(String joint, List elements)
  4. join(String sep, List a)
  5. join(String separator, Collection list)
  6. join(String separator, List items)
  7. join(String separator, List objs)
  8. join(String separator, List elements)
  9. join(String separator, List items)