Java Collection to String collectionToString( Collection collection)

Here you can find the source of collectionToString( Collection collection)

Description

Transform a collection of objects to a whitespace delimited String.

License

Apache License

Parameter

Parameter Description
collection the collection to transform.

Return

a String with all the elements of the collection.

Declaration

public static String collectionToString(
        Collection<? extends Object> collection) 

Method Source Code

//package com.java2s;
/**//from w w w .  j  a  v  a 2 s  . co m
 *
 * Copyright 2003-2007 Jive Software, 2016 Florian Schmaus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Transform a collection of objects to a whitespace delimited String.
     *
     * @param collection the collection to transform.
     * @return a String with all the elements of the collection.
     */
    public static String collectionToString(
            Collection<? extends Object> collection) {
        return toStringBuilder(collection, " ").toString();
    }

    /**
     * Transform a collection of objects to a delimited String.
     *
     * @param collection the collection to transform.
     * @param delimiter the delimiter used to delimit the Strings.
     * @return a StringBuilder with all the elements of the collection.
     */
    public static StringBuilder toStringBuilder(
            Collection<? extends Object> collection, String delimiter) {
        StringBuilder sb = new StringBuilder(collection.size() * 20);
        for (Iterator<? extends Object> it = collection.iterator(); it
                .hasNext();) {
            Object cs = it.next();
            sb.append(cs);
            if (it.hasNext()) {
                sb.append(delimiter);
            }
        }
        return sb;
    }
}

Related

  1. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  2. collectionToDelimitedString(Collection values, String delimiter)
  3. collectionToStr(Collection collection)
  4. CollectionToStr(Collection coll)
  5. collectionToStr(Collection collection)
  6. collectionToString(@SuppressWarnings("rawtypes") Collection coll)
  7. collectionToString(Collection c)
  8. collectionToString(Collection c, String delim)
  9. collectionToString(Collection c, String spilt)