Java Collection Join join(Collection strings, String separator)

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

Description

Concatenate multiple strings by inserting a separator.

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 * Copyright (c) 2004-2011 Oracle Corporation.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: /*from   w w  w. j  a va2 s  . c o m*/
 *
 *    Kohsuke Kawaguchi, Winston Prakash
 *     
 *
 *******************************************************************************/

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
    /**
     * Concatenate multiple strings by inserting a separator.
     */
    public static String join(Collection<?> strings, String separator) {
        StringBuilder buf = new StringBuilder();
        boolean first = true;
        for (Object s : strings) {
            if (first) {
                first = false;
            } else {
                buf.append(separator);
            }
            buf.append(s);
        }
        return buf.toString();
    }

    /**
     * Combines all the given collections into a single list.
     */
    public static <T> List<T> join(Collection<? extends T>... items) {
        int size = 0;
        for (Collection<? extends T> item : items) {
            size += item.size();
        }
        List<T> r = new ArrayList<T>(size);
        for (Collection<? extends T> item : items) {
            r.addAll(item);
        }
        return r;
    }
}

Related

  1. join(Collection s, String delimiter)
  2. join(Collection strings, String delimiter)
  3. join(Collection strings, String delimiter)
  4. join(Collection strings, String separator)
  5. join(Collection strings, String separator)
  6. join(Collection strs)
  7. join(Collection toJoin, String joinBy)
  8. join(Collection tokens, String d)
  9. join(Collection values)