Java Collection Print print(java.util.Collection collection)

Here you can find the source of print(java.util.Collection collection)

Description

Return a String representing the elements of collection in a human readable format.

License

GNU General Public License

Return

A String representing collection.

Declaration

public static final String print(java.util.Collection collection) 

Method Source Code

//package com.java2s;
// Licensed under the terms of the GNU GPL; see COPYING for details.

public class Main {
    /** Return a <code>String</code> representing the elements of
    <code>collection</code> in a human readable format.  
    <BR> <B>effects:</B> Iterates over <code>collection</code>,
       calling <code>toString()</code> on each element and
    appending the result to a <code>String</code>.  The format
    of the returned <code>String</code> is 
    <NOBR>{ elem1, elem2, ... , elemN }</NOBR>
    @return A <code>String</code> representing
      <code>collection</code>. 
    *///from  w w w.  ja  v  a2  s. c o  m
    public static final String print(java.util.Collection collection) {
        StringBuffer sb = new StringBuffer("{ ");
        java.util.Iterator iter = collection.iterator();
        while (iter.hasNext()) {
            Object elem = iter.next();
            sb.append(elem.toString());
            if (iter.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append(" }");
        return sb.toString();
    }
}

Related

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