Java Collection Join join(Collection c, String concatinator)

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

Description

Concatenates all elements in the given collection c into a single string with the separator

License

Apache License

Parameter

Parameter Description
c a collection of elements to concatenate
concatinator a concatenator: ex. ", ", "." etc.

Return

a concatenated string

Declaration

public static <T> String join(Collection<T> c, String concatinator) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2004 Taro L. Saito/*from  w  w w. jav a2s  .  c  o  m*/
 *
 *  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;

import java.util.Iterator;

public class Main {
    /**
     * Concatenates all elements in the given collection c into a single string
     * with the separator
     * 
     * @param c
     *            a collection of elements to concatenate
     * @param concatinator
     *            a concatenator: ex. ", ", "." etc.
     * @return a concatenated string
     */
    public static <T> String join(Collection<T> c, String concatinator) {
        if (c == null)
            return "";
        int size = c.size();
        if (size == 0)
            return "";

        Iterator<T> it = c.iterator();
        StringBuilder buf = new StringBuilder();
        for (int i = 0; it.hasNext() && i < size - 1; i++) {
            Object data = it.next();
            if (data != null)
                buf.append(data.toString());
            else
                buf.append("null");
            buf.append(concatinator);
        }
        Object lastData = it.next();
        if (lastData != null)
            buf.append(lastData.toString());
        else
            buf.append("null");
        return buf.toString();
    }

    /**
     * Concatenates all elements in the given array c into a single string with
     * the separator
     * 
     * @param c
     *            an array of elements to concatenate
     * @param concatinator
     *            a concatenator: ex. ", ", "." etc.
     * @return the concatenated string
     */
    public static String join(Object[] c, String concatinator) {
        if (c == null)
            return "";
        int size = c.length;
        if (size == 0)
            return "";

        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < size - 1; i++) {
            Object data = c[i];
            buf.append(data != null ? data.toString() : "");
            buf.append(concatinator);
        }
        buf.append(c[size - 1]);
        return buf.toString();
    }
}

Related

  1. join(Collection strings, String token)
  2. join(Collection strs, String sep)
  3. join(Collection thisPath, String _delim)
  4. join(Collection values)
  5. join(Collection words, String character)
  6. join(Collection coll, String separator)
  7. join(Collection collec, String delimiter)
  8. join(Collection collection, CharSequence joinedBy)
  9. join(Collection collection, String joinString)