Java Collection to String toString(Collection c)

Here you can find the source of toString(Collection c)

Description

Same as #toString(Collection,String) toString separated by ", ".

License

Open Source License

Parameter

Parameter Description
c a parameter

Declaration

public static String toString(Collection c) 

Method Source Code


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

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**/*  w  ww  .j a  va2  s.c  o m*/
     * Same as {@link #toString(Collection,String) toString}
     * separated by ", ".
     * 
     * @param c
     * @return
     */
    public static String toString(Collection c) {
        return toString(c, ", ");
    }

    public static String toString(Object[] objs) {
        return toString(Arrays.asList(objs));
    }

    public static String toString(Object[] objs, String separatedBy) {
        if (objs != null) {
            return toString(Arrays.asList(objs), separatedBy);
        } else {
            return "";
        }
    }

    /**
     * Returns a String representation of this collection.
     * The items are converted to strings using String.format(frmtPattern).
     * If frmtPattern is given, the items will be formatted as
     * String.format(Object, frmtPattern), else it will use String.valueOf(Object).
     * If separatedby is given, the items will be separated by that string.
     *
     * @param c
     * @param separatedBy
     * @return
     */
    public static String toString(Collection c, String separatedBy/*, String frmtPattern*/) {

        if (c == null || c.size() == 0)
            return "";

        //        boolean usePattern = frmtPattern != null;
        StringBuffer sb = new StringBuffer();
        for (Iterator itr = c.iterator(); itr.hasNext();) {
            Object o = itr.next();
            //            String s = usePattern ? String.format(frmtPattern, o) : String.valueOf(o);
            String s = String.valueOf(o);
            sb.append(s);
            if (separatedBy != null && itr.hasNext()) {
                sb.append(separatedBy);
            }
        }

        return sb.toString();
    }

    /**
     * takes an int array and returns a List of Integer backed by the array
     * @param a
     * @return
     */
    public static List<Integer> asList(final int[] a) {
        return new AbstractList<Integer>() {
            public Integer get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Integer set(int i, Integer val) {
                Integer oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    /**
     * takes a float array and returns a List of Float backed by the array
     * @param a
     * @return
     */
    public static List<Float> asList(final float[] a) {
        return new AbstractList<Float>() {
            public Float get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Float set(int i, Integer val) {
                Float oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    public static <T> List<T> asList(T... items) {

        ArrayList<T> list = new ArrayList<T>(items.length);
        if (items != null) {
            for (T item : items) {
                list.add(item);
            }
        }
        return list;
    }
}

Related

  1. stringify(Collection collection)
  2. stringify(Collection collection)
  3. toStr(Collection collection)
  4. toStr(Collection elements)
  5. toString(Collection c)
  6. toString(Collection c, String start, String separator, String end)
  7. toString(Collection collection, String separator, int fixedLength)
  8. toString(Collection objects)
  9. toString(Collection setOfTests)