Here you can find the source of toString(final Collection> collection, final String separator)
Parameter | Description |
---|---|
collection | the collection to represent as text |
separator | the separator to include between elements of the collection |
public static String toString(final Collection<?> collection, final String separator)
//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(); } }