Java Collection Join join(final Collection collection, final String separator)

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

Description

join

License

Open Source License

Declaration

public static String join(final Collection<String> collection, final String separator) 

Method Source Code

//package com.java2s;
/*/*from   w  w w.j ava 2 s .  c  om*/
 *
 * SchemaCrawler
 * http://sourceforge.net/projects/schemacrawler
 * Copyright (c) 2000-2015, Sualeh Fatehi.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Main {
    public static String join(final Collection<String> collection, final String separator) {
        if (collection == null || collection.isEmpty()) {
            return null;
        }

        final StringBuilder buffer = new StringBuilder();
        for (final Iterator<String> iterator = collection.iterator(); iterator.hasNext();) {
            buffer.append(iterator.next());
            if (iterator.hasNext()) {
                buffer.append(separator);
            }
        }

        return buffer.toString();
    }

    public static String join(final Map<String, String> map, final String separator) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        final StringBuilder buffer = new StringBuilder();
        final Set<Entry<String, String>> entrySet = map.entrySet();
        for (final Iterator<Entry<String, String>> iterator = entrySet.iterator(); iterator.hasNext();) {
            final Entry<String, String> entry = iterator.next();
            buffer.append(entry.getKey()).append("=").append(entry.getValue());
            if (iterator.hasNext()) {
                buffer.append(separator);
            }
        }

        return buffer.toString();
    }

    public static String join(final String[] collection, final String separator) {
        return join(Arrays.asList(collection), separator);
    }
}

Related

  1. join(final Collection list, final String inbetween)
  2. join(final Collection strings, String delimeter)
  3. join(final Collection values, final String separator)
  4. join(final Collection aStrings, final String aSeparator)
  5. join(final Collection chunks, final String delimiter)
  6. join(final Collection list, final String separator)
  7. join(final Collection seq, final String delimiter)
  8. join(final Collection strs, final String delimiter)
  9. join(final Collection toJoin)

  10. HOME | Copyright © www.java2s.com 2016