Java Collection Convert toDelimitedString(final Collection coll, final String delim)

Here you can find the source of toDelimitedString(final Collection coll, final String delim)

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 ",")

Declaration

public static String toDelimitedString(final Collection coll, final String delim) 

Method Source Code


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

import java.util.*;

public class Main {
    /**//from w w w  .j  av  a  2s .  co  m
     * Convenience method to return a String array as a delimited (e.g. CSV)
     * String. E.g. useful for toString() implementations.
     *
     * @param arr   array to display. Elements may be of any type (toString will
     *              be called on each element).
     * @param delim delimiter to use (probably a ",")
     */
    public static String toDelimitedString(final Object[] arr, final String delim) {
        if (arr == null) {
            return "";
        }

        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    public static String toDelimitedString(final Object[] arr, final String delim, int startIndex, int endIndex) {
        if (arr == null) {
            return "";
        }

        final StringBuilder sb = new StringBuilder();
        endIndex = arr.length - 1 < endIndex ? arr.length - 1 : endIndex;
        for (int i = startIndex; i < endIndex + 1; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * 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 ",")
     */
    public static String toDelimitedString(final Collection coll, final String delim) {
        return tooDelimitedString(coll, delim, "", "");
    }

    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();
    }

    /**
     * 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();
    }
}

Related

  1. toCsvString(Collection collection)
  2. toCSVString(Collection theCollection, String theSeparator)
  3. toDelimitedList(Collection values, String delimiter)
  4. toDelimitedString(Collection c, String delimiter)
  5. toDelimitedString(Collection coll, String delim)
  6. toDoubles(Collection numbers)
  7. toEnumeration(final Collection collection)
  8. toEscapedStringWithDelimiters(Collection objects, String delim)
  9. toggleEntry(T entry, Collection collection)