Java Iterator toString(Iterator iterObjects)

Here you can find the source of toString(Iterator iterObjects)

Description

to String

License

Open Source License

Declaration

public static String toString(Iterator<?> iterObjects) 

Method Source Code

//package com.java2s;
import java.util.Iterator;

public class Main {
    /**//from ww  w  .  ja va  2 s  . c  o m
     * Generates a <code>String</code> value from the given <code>Iterator</code> of the form:
     * '[' object.toString() ',' object.toString() ',' ... ']'
     * 
     * @param iterObjects the <code>Iterator</code> to dump into a <code>String</code>
     * @param allowEmptyLists if true, show empty lists as "[]" otherwise return null
     * @return the <code>String</code> representation of the <code>Iterator</code> or null if the <code>Iterator</code> is null 
     * (or empty if allowEmptyLists is false)
     */
    public static String toString(Iterator<?> iterObjects,
            boolean allowEmptyLists) {
        if (iterObjects == null
                || (!allowEmptyLists && !iterObjects.hasNext())) {
            return null;
        } else {
            StringBuilder stringBuilder = new StringBuilder("[");
            if (iterObjects.hasNext()) {
                stringBuilder.append(iterObjects.next().toString());
                while (iterObjects.hasNext()) {
                    stringBuilder.append(',');
                    stringBuilder.append(iterObjects.next().toString());
                }
            }
            stringBuilder.append(']');
            return stringBuilder.toString();
        }
    }

    public static String toString(short[] a) {
        StringBuilder stringBuilder = new StringBuilder("[");
        if (a != null && a.length > 0) {
            stringBuilder.append(a[0]);
            for (int i = 1; i < a.length; i++) {
                stringBuilder.append(',');
                stringBuilder.append(a[i]);
            }
        }
        stringBuilder.append(']');
        return stringBuilder.toString();
    }

    public static String toString(byte[] a) {
        StringBuilder stringBuilder = new StringBuilder("[");
        if (a != null && a.length > 0) {
            stringBuilder.append(a[0]);
            for (int i = 1; i < a.length; i++) {
                stringBuilder.append(',');
                stringBuilder.append(a[i]);
            }
        }
        stringBuilder.append(']');
        return stringBuilder.toString();
    }

    public static String toString(Iterator<?> iterObjects) {
        return toString(iterObjects, false);
    }
}

Related

  1. toIterator(Collection col)
  2. toIterator(final T[] data)
  3. toMap(final Iterator> iterator)
  4. toObjectIterator(Object[] objects)
  5. toSet(Iterator iteration)
  6. toString(Iterator tokens)
  7. toStringArray(Iterator iterator)
  8. unwrapIterator(Iterator iter)