Java Collection Max toString(Collection collection, int maxLen)

Here you can find the source of toString(Collection collection, int maxLen)

Description

Converts given collection to string of given maximum count of items.

License

Open Source License

Parameter

Parameter Description
collection a parameter
maxLen a parameter

Declaration

public static String toString(Collection<?> collection, int maxLen) 

Method Source Code

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

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/* w  w w.  j  a  v  a 2s.c o m*/
     * Converts given collection to string of given maximum count of items.
     * 
     * @param collection
     * @param maxLen
     * @return
     */
    public static String toString(Collection<?> collection, int maxLen) {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        int i = 0;
        for (Iterator<?> iterator = collection.iterator(); iterator.hasNext() && i < maxLen; i++) {
            if (i > 0)
                builder.append(", ");
            builder.append(iterator.next());
        }
        builder.append("]");
        return builder.toString();
    }
}

Related

  1. max(final Collection collection)
  2. maxLength(Collection strings)
  3. maxOr(Collection values, T defaultVal)
  4. minOrMax(int sign, Collection values)
  5. partitionFixed(int maxNumChunks, Collection coll)
  6. toString(Collection entries, int max)
  7. transfer(Collection source, Collection dest, int maxElems)
  8. truncate(Collection coll, int maximumSize)
  9. valueOfCollection(Collection c, int maxLength)