Android Collection to String Convert toString(final Collection collection)

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

Description

Builds a string for a collection of objects.

License

Open Source License

Parameter

Parameter Description
collection the collection to stringify. can be null

Return

a string representing the collection

Declaration

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

Method Source Code

//package com.java2s;
/*/*from w w w .  j  a v  a 2  s  .  co m*/
 * Copyright (C) 2013 nohana, Inc. All rights reserved.
 *
 * 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;

public class Main {
    /**
     * Builds a string for a collection of objects.
     * Each element of the collection if stringified according to {@link String#valueOf(Object)}
     * rules.
     * @param collection the collection to stringify. can be null
     * @return a string representing the collection
     * @see String#valueOf(Object)
     */
    public static String toString(final Collection<?> collection) {
        if (collection == null)
            return String.valueOf(collection);
        //else
        final StringBuilder result = new StringBuilder().append("{");
        String elementSeparator = "";
        for (final Object element : collection) {
            result.append(elementSeparator);
            result.append(String.valueOf(element));
            elementSeparator = ",";
        }
        result.append("}");
        return result.toString();
    }
}

Related

  1. toString(final Collection collection, final char token, final boolean includeSpace)
  2. toString(final Collection collection, final char token, final boolean includeSpace)
  3. toString(Collection arrays)