Java Collection to String collectionToString(String prefix, Collection list)

Here you can find the source of collectionToString(String prefix, Collection list)

Description

collection To String

License

Apache License

Declaration

private static String collectionToString(String prefix,
            Collection<?> list) 

Method Source Code

//package com.java2s;
/*/*from   w w w.j a v a2 s .  c o m*/
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    private static String collectionToString(String prefix,
            Collection<?> list) {
        if (list == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        sb.append('[');
        boolean first = true;
        for (Object item : list) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(item instanceof Object[] ? arrayToString((Object[]) item)
                    : item);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(double[] xs) {
        StringBuilder sb = new StringBuilder();

        sb.append('[');
        for (double x : xs) {
            if (sb.length() != 1)
                sb.append(", ");
            sb.append(x);
        }
        sb.append(']');

        return sb.toString();
    }

    public static String arrayToString(Object[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(Object[][] arrayArray) {
        if (arrayArray == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        boolean first = true;
        for (Object[] array : arrayArray) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(arrayToString(array));
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(int[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(int[][] xss) {
        if (xss == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < xss.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(arrayToString(xss[i]));
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(char[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(boolean[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }
}

Related

  1. collectionToString(final Collection collection, final String separator)
  2. collectionToString(final Collection elements, final String separator)
  3. collectionToString(final String separator, final Collection coll)
  4. collectionToString(Iterable c)
  5. collectionToString(Object[] objects)
  6. collectionToStringArray(Collection holder)
  7. collectionToStringArray(Collection objs)
  8. collectionToStringArray(List values)
  9. collectionToStringList(Collection c)