Java Collection to String toString(final Collection collection, final String separator)

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

Description

Create a textual representation of the given collection, separating the individual elements by the provided separator.

License

Open Source License

Parameter

Parameter Description
collection the collection to represent as text
separator the separator to include between elements of the collection

Return

the colleciton's textual representation (or an empty String if the collection is null or empty)

Declaration

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

Method Source Code

//package com.java2s;
/*// ww  w  .  ja  va 2  s. c  o m
   Copyright (C) 2016 HermeneutiX.org
    
   This file is part of SciToS.
    
   SciToS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   SciToS 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 General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with SciToS. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;

public class Main {
    /**
     * Create a textual representation of the given collection, separating the individual elements by the provided separator.
     * 
     * @param collection
     *            the collection to represent as text
     * @param separator
     *            the separator to include between elements of the collection
     * @return the colleciton's textual representation (or an empty String if the collection is {@code null} or empty)
     */
    public static String toString(final Collection<?> collection, final String separator) {
        if (collection == null || collection.isEmpty()) {
            // just return an empty String if the collection is null or empty
            return "";
        }
        if (separator == null) {
            // fall back to the collection's toString() method if no custom separator has been specified
            return collection.toString();
        }
        // guess at a meaningful initial size
        final StringBuilder builder = new StringBuilder(collection.size() * (16 + separator.length()));
        boolean addSeparator = false;
        // iterate over the individual elements
        for (final Object collectionElement : collection) {
            if (addSeparator) {
                builder.append(separator);
            } else {
                addSeparator = true;
            }
            // null elements are treated as empty Strings
            if (collectionElement != null) {
                builder.append(collectionElement.toString());
            }
        }
        return builder.toString();
    }
}

Related

  1. toString(Collection collection)
  2. toString(Collection collection, String divider)
  3. toString(Collection list, String delimeter)
  4. toString(final Collection collection)
  5. toString(final Collection collection)
  6. toString(final Collection objs)
  7. toString(final Collection values)
  8. toString(final Collection list, final String delimiter)
  9. toString(final Collection c)