Java Collection Join join(Collection items, String join)

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

Description

Join elements of a String collection into a single string.

License

Apache License

Parameter

Parameter Description
items the String items to serialise into a string
join the String used between each string item in the list

Return

A single string where the items are appended together with "join" seperators

Declaration

public static String join(Collection<String> items, String join) 

Method Source Code

//package com.java2s;
/*/*from w  w  w .  ja  v a 2  s  .  co  m*/
 * Copyright 2010-2011 Heads Up Development Ltd.
 *
 * 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.Collection;

public class Main {
    /**
     * Join elements of a String collection into a single string.
     * The provided join String is used to between each item (i.e. ",")
     *
     * @param items the String items to serialise into a string
     * @param join the String used between each string item in the list
     * @return A single string where the items are appended together with "join" seperators
     */
    public static String join(Collection<String> items, String join) {
        StringBuilder list = new StringBuilder();
        boolean first = true;
        for (String type : items) {
            if (!first) {
                list.append(join);
            }

            list.append(type);
            first = false;
        }

        return list.toString();
    }
}

Related

  1. join(Collection collection, String separator)
  2. join(Collection collection, String separator)
  3. join(Collection elements)
  4. join(Collection in, String joinS)
  5. join(Collection items)
  6. join(Collection items, String prefix, String suffix, String itemPrefix, String itemSuffix)
  7. join(Collection list)
  8. join(Collection list, String conjunction)
  9. join(Collection packs)