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

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

Description

Creates a string representation of the elements in the collection separated by separator.

License

Open Source License

Parameter

Parameter Description
collection to print
separator to use between elements

Return

String representing the collection

Declaration

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

Method Source Code

//package com.java2s;
/*/*from  w w w.j a  va 2s.  co  m*/
 * Copyright 2015-2018 Ping Identity Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Creates a string representation of the elements in the
     * <code>collection</code> separated by <code>separator</code>.
     *
     * @param collection to print
     * @param separator to use between elements
     *
     * @return String representing the collection
     */
    public static String collectionToString(final Collection<?> collection, final String separator) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<?> iter = collection.iterator(); iter.hasNext();) {
            sb.append(iter.next());
            if (iter.hasNext()) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }
}

Related

  1. collectionToString(Collection collection)
  2. collectionToString(Collection list)
  3. collectionToString(final Collection collection, final String recordSeparator)
  4. collectionToString(final Collection aCol, final String aSepLines)
  5. collectionToString(final Collection c)
  6. collectionToString(final Collection elements, final String separator)
  7. collectionToString(final String separator, final Collection coll)
  8. collectionToString(Iterable c)
  9. collectionToString(Object[] objects)