Java Collection Join joinCollection(Collection collection, char delimiter)

Here you can find the source of joinCollection(Collection collection, char delimiter)

Description

Joins the items of a collection together with the argument delimiter followed by a space.

License

Open Source License

Parameter

Parameter Description
collection The collection to join
delimiter The delimiter to place between each item in the collection.

Return

The concatenated string of all items in the collection joined with the argument delimiter.

Declaration

public static String joinCollection(Collection<?> collection, char delimiter) 

Method Source Code

//package com.java2s;
/**//from  www.j a  v a2s  . c om
 * This file is a component of the p.opulo.us project.
 *
 * Copyright (c) 2013 Jason Campos <jcampos8782@gmail.com>, Shaun Guth, Ash Islam
 * All Rights Reserved.
 *
 * This software is licensed under The MIT License (MIT)
 * http://opensource.org/licenses/MIT
 */

import java.util.Collection;

public class Main {
    /**
     * Joins the items of a collection together with the argument delimiter followed by a space. 
     * Invokes the toString() method on each item in the collection. 
     * 
     * @param collection The collection to join
     * @param delimiter The delimiter to place between each item in the collection.
     * @return The concatenated string of all items in the collection joined with the argument delimiter.
     */
    public static String joinCollection(Collection<?> collection, char delimiter) {
        return joinCollection(collection, delimiter, true);
    }

    /**
     * Joins the items of a collection together with the argument delimiter optionally followed by a space. 
     * Invokes the toString() method on each item in the collection. 
     * 
     * @param collection The collection to join
     * @param delimiter The delimiter to place between each item in the collection.
     * @param addSpace {@code true} to add a space after the delimiter. {@code false} for no space.
     * @return The concatenated string of all items in the collection joined with the argument delimiter.
     */
    public static String joinCollection(Collection<?> collection, char delimiter, boolean addSpace) {
        StringBuilder result = new StringBuilder();
        for (Object o : collection) {
            result.append(o.toString() + delimiter + (addSpace ? " " : ""));
        }

        return result.length() > 0 ? result.substring(0, result.length() - (addSpace ? 2 : 1)) : "";
    }
}

Related

  1. join2(Collection list, String delimiter)
  2. joinAndDelimit(final Collection strings, final String sep, final String delim)
  3. joinAsString(Collection collection, String separator)
  4. joinAsStrings(Collection values, String separator)
  5. joinCol(Collection lst, String delim)
  6. joinCollection(Collection a)
  7. joinCollections(final Collection target, final Collection... collections)
  8. joinCollectionToString(Collection list, String str)
  9. joinCommaDelimitedList(Collection list)