Java Collection Convert tooDelimitedString(final Collection coll, final String delim, final String prefix, final String suffix)

Here you can find the source of tooDelimitedString(final Collection coll, final String delim, final String prefix, final String suffix)

Description

Convenience method to return a Collection as a delimited (e.g.

License

LGPL

Parameter

Parameter Description
coll Collection to display
delim delimiter to use (probably a ",")
prefix string to start each element with
suffix string to end each element with

Declaration

public static String tooDelimitedString(final Collection coll, final String delim, final String prefix,
        final String suffix) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    /**//w  w w  . ja v  a  2 s. c o m
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. E.g. useful for toString() implementations.
     *
     * @param coll   Collection to display
     * @param delim  delimiter to use (probably a ",")
     * @param prefix string to start each element with
     * @param suffix string to end each element with
     */
    public static String tooDelimitedString(final Collection coll, final String delim, final String prefix,
            final String suffix) {
        if (coll == null) {
            return "";
        }

        final StringBuilder sb = new StringBuilder();
        Iterator it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(prefix).append(it.next()).append(suffix);
            i++;
        }
        return sb.toString();
    }

    public static String toString(final Collection list) {
        final StringBuilder sb = new StringBuilder();
        if (null != list) {
            for (final Object item : list) {
                if (null != item) {
                    sb.append(item);
                }
            }
        }
        return sb.toString();
    }

    public static String toString(final Object[] list) {
        final StringBuilder sb = new StringBuilder();
        if (null != list) {
            for (final Object item : list) {
                if (null != item) {
                    sb.append(item);
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. toLowerCase(Collection values)
  2. toMap(Collection names, Object[] parameters)
  3. toMatrix(Collection collection)
  4. toObjectArray(Collection list)
  5. toObjectCollection(Object v)
  6. toPrimitiveArray(final Collection values)
  7. toPrimitiveIntegerArray(Collection collection)
  8. toReadableString(Collection collection)
  9. toSeparatedString(Collection items, String separator)

  10. HOME | Copyright © www.java2s.com 2016