Java Collection Join join(String separator, Collection objs)

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

Description

Stringify all the elements in the given collection, and join them with inserting the given separator.

License

Open Source License

Parameter

Parameter Description
separator A separater to be inserted.
objs A collection of objects to be joined.

Return

A joined string.

Declaration

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

Method Source Code

//package com.java2s;
/*/* w ww .  java2  s  .  co  m*/
 * Copyright (c) 2014-2015 NEC 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
 */

import java.util.Collection;

public class Main {
    /**
     * Stringify the given objects, and join them with inserting the given
     * separator.
     *
     * @param separator  A separater to be inserted.
     * @param objs       Objects to be joined.
     * @return  A joined string.
     */
    public static String join(String separator, Object... objs) {
        StringBuilder builder = new StringBuilder();
        String sep = "";
        if (objs != null) {
            for (Object o : objs) {
                builder.append(sep).append(String.valueOf(o));
                sep = separator;
            }
        }

        return builder.toString();
    }

    /**
     * Stringify all the elements in the given collection, and join them with
     * inserting the given separator.
     *
     * @param separator  A separater to be inserted.
     * @param objs       A collection of objects to be joined.
     * @return  A joined string.
     */
    public static String join(String separator, Collection<?> objs) {
        StringBuilder builder = new StringBuilder();
        String sep = "";
        if (objs != null) {
            for (Object o : objs) {
                builder.append(sep).append(String.valueOf(o));
                sep = separator;
            }
        }

        return builder.toString();
    }
}

Related

  1. join(String sep, Collection strs)
  2. join(String separator, Collection c)
  3. join(String separator, Collection c)
  4. join(String separator, Collection items)
  5. join(String separator, Collection items)
  6. join(String separator, Collection list)
  7. join(String separator, Collection parts)
  8. join(String separator, Collection strings)
  9. join(String separator, Collection objects)