Java Collection Join joinToStringBuilder(Collection collection, String separator)

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

Description

Concatenates the Collection of Objects (converted to Strings using .toString()) into a StringBuilder using the separator as the delimiter.

License

Open Source License

Parameter

Parameter Description
collection An Collection of objects
separator The separating character

Return

A 'separator' separated StringBuilder

Declaration

public static StringBuilder joinToStringBuilder(Collection<?> collection, String separator) 

Method Source Code


//package com.java2s;
/*//from ww  w . j  a v a  2  s .  c om
 * Copyright (c) Thomas Parker, 2007, 2008.
 *  portions derived from CoreUtility.java
 *    Copyright 2002 (C) Bryan McRoberts <merton_monk@yahoo.com>
 * 
 * This program 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 program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * 
 */

import java.util.Collection;

public class Main {
    /**
     * Concatenates the Collection of Objects (converted to Strings using
     * .toString()) into a StringBuilder using the separator as the delimiter.
     * 
     * This method is value-semantic and will not modify or maintain a reference
     * to the given Collection of Objects. Ownership of the returned
     * StringBuilder is transferred to the calling object. No reference to the
     * StringBuilder is maintained by StringUtil.
     * 
     * @param collection
     *            An Collection of objects
     * @param separator
     *            The separating character
     * @return A 'separator' separated StringBuilder
     */
    public static StringBuilder joinToStringBuilder(Collection<?> collection, String separator) {
        if (collection == null) {
            return new StringBuilder();
        }

        StringBuilder result = new StringBuilder(collection.size() * 10);

        boolean needjoin = false;

        for (Object obj : collection) {
            if (needjoin) {
                result.append(separator);
            }
            needjoin = true;
            result.append(obj.toString());
        }

        return result;
    }
}

Related

  1. joinStrings(Collection strings)
  2. joinStrings(String delim, Collection strs)
  3. joinStringValues(Collection values)
  4. joinTo(Collection data, String sep, StringBuilder sb)
  5. joinTogether(Collection items, String delim)
  6. joinValues(Collection values, String separator)
  7. roundjoin(Collection s, String delimiter)
  8. stringJoin(Collection list, String delim)
  9. stringJoin(java.util.Collection s, String delimiter)

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