Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Constructs a string representation of the specified collection.
     * 
     * <p>Empty collections are represented as {@code "[ ]"}. A collection with a single
     * element would be represented as {@code "[ item ]"}, where <em>"item"</em> is the
     * value of {@link String#valueOf(Object) String.valueOf(theOneItem)}. Collections
     * with multiple items follow the same pattern, with multiple items separated by a
     * comma and a single space, like so: {@code "[ item1, item2, item3 ]"}.
     *
     * @param coll the collection
     * @return a string representation of {@code coll}
     */
    public static String toString(Iterable<?> coll) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        boolean first = true;
        for (Object item : coll) {
            if (first) {
                first = false;
            } else {
                sb.append(",");
            }
            sb.append(" ");
            if (item == coll) {
                // don't overflow stack if collection contains itself
                sb.append("( this collection )");
            } else {
                sb.append(String.valueOf(item));
            }
        }
        sb.append(" ]");
        return sb.toString();
    }
}