Java Collection Print print(Collection elements)

Here you can find the source of print(Collection elements)

Description

Returns a String representation of given collection, separated by comma and surrounded by square brackets.

License

Open Source License

Parameter

Parameter Description
elements a parameter

Return

a String

Declaration

public static <T> String print(Collection<T> elements) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//from ww  w . j  a  va2  s  .c om
     * Returns a String representation of given elements, separated by comma and surrounded by
     * square brackets.
     * @param elements
     * @return a String
     */
    public static <T> String print(T[] elements) {
        return print(elements, ",", "[", "]");
    }

    /**
     * Returns a String representation of given elements, with given separator and not surrounded by
     * any other characters.
     * @param elements
     * @param separator
     * @return a String
     */
    public static <T> String print(T[] elements, String separator) {
        return print(elements, separator, "", "");
    }

    /**
     * Returns a String representation of given elements, using given separator and surrounding
     * strings.
     * @param elements the elements to print, they are printed in the order they were specified
     * @param separator the separator string
     * @param begin the prefix string, appended at first
     * @param end the postfix string, appended at last
     * @return a String
     */
    public static <T> String print(T[] elements, String separator, String begin, String end) {
        StringBuilder ret = new StringBuilder();
        if (elements.length == 0) {
            ret.append(begin).append(end);
            return ret.toString();
        } else {
            ret.append(begin);
            boolean first = true;
            for (T element : elements)
                if (first) {
                    ret.append(element);
                    first = false;
                } else
                    ret.append(separator).append(element);
            ret.append(end);
            return ret.toString();
        }
    }

    /**
     * Returns a String representation of given collection, separated by comma and surrounded by
     * square brackets.
     * @param elements
     * @return a String
     */
    public static <T> String print(Collection<T> elements) {
        return print(elements, ",", "[", "]");
    }

    /**
     * Returns a String representation of given collection, with given separator and not surrounded
     * by any other characters.
     * @param elements
     * @param separator
     * @return a String
     */
    public static <T> String print(Collection<T> elements, String separator) {
        return print(elements, separator, "", "");
    }

    /**
     * Returns a String representation of given collection, using given separator and surrounding
     * strings.
     * @param elements the elements to print, they are printed in the order they were specified
     * @param separator the separator string
     * @param begin the prefix string, appended at first
     * @param end the postfix string, appended at last
     * @return a String
     */
    public static <T> String print(Collection<T> elements, String separator, String begin, String end) {
        StringBuilder ret = new StringBuilder();
        if (elements.isEmpty()) {
            ret.append(begin).append(end);
            return ret.toString();
        } else {
            ret.append(begin);
            boolean first = true;
            for (T element : elements)
                if (first) {
                    ret.append(element);
                    first = false;
                } else
                    ret.append(separator).append(element);
            ret.append(end);
            return ret.toString();
        }
    }
}

Related

  1. print(Collection c)
  2. print(Collection collection, String delim)
  3. print(Collection c, String separator)
  4. print(Collection c)
  5. print(Collection coll)
  6. print(java.util.Collection collection)
  7. printCollection(Collection c)
  8. printCollection(Collection col)
  9. printCollection(Collection collection)