Java Collection to String toString(final Collection values)

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

Description

to String

License

Open Source License

Parameter

Parameter Description
values a parameter

Return

String

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Institute of Biotechnology
 * University of Manchester/*from  w  w  w.  j av  a 2s.  c  om*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2013 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     */
    private static final String COLLECTION_SEPARATOR = ", ";
    /**
     * 
     */
    private static final int COLLECTION_SEPARATOR_LENGTH = COLLECTION_SEPARATOR
            .length();

    /**
     * 
     * @param values
     * @return String
     */
    public static String toString(final Collection<?> values) {
        final StringBuilder builder = new StringBuilder();

        for (Object value : values) {
            builder.append(value);
            builder.append(COLLECTION_SEPARATOR);
        }

        if (values.size() > 0) {
            builder.setLength(builder.length()
                    - COLLECTION_SEPARATOR_LENGTH);
        }

        return builder.toString();
    }
}

Related

  1. toString(Collection list, String delimeter)
  2. toString(final Collection collection)
  3. toString(final Collection collection)
  4. toString(final Collection collection, final String separator)
  5. toString(final Collection objs)
  6. toString(final Collection list, final String delimiter)
  7. toString(final Collection c)
  8. toString(String[] collection, char separator)
  9. toStrings(Collection vals)