Java Collection to String toString(Collection itemList)

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

Description

to String

License

Open Source License

Declaration

public static String toString(Collection<?> itemList) 

Method Source Code


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

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static String toString(Collection<?> itemList) {
        return printf("[%s]", join(itemList, ", ", "\"%s\""));
    }//from ww w  . j a  v a  2  s .  c o  m

    public static String toString(Map<?, ?> itemMap) {
        ArrayList<String> entryStrings = new ArrayList<String>();

        for (Map.Entry<?, ?> entry : itemMap.entrySet()) {
            String entryString = printf("%s: \"%s\"", entry.getKey(), entry.getValue());
            entryStrings.add(entryString);
        }

        return printf("{%s}", join(entryStrings, ", "));
    }

    public static String printf(String format, Object... args) {
        return String.format(format, args);
    }

    public static String join(Collection<?> stringList, String seperator) {
        return join(stringList, seperator, "%s");
    }

    public static String join(Collection<?> stringList, String seperator, String itemFormat) {
        StringBuffer buffer = new StringBuffer();

        Iterator<?> it = stringList.iterator();

        for (int i = 0; it.hasNext(); i++) {
            if (i > 0) {
                buffer.append(seperator);
            }

            buffer.append(printf(itemFormat, it.next().toString()));
        }

        return buffer.toString();
    }
}

Related

  1. toString(Collection collection)
  2. toString(Collection collection)
  3. toString(Collection collection)
  4. toString(Collection collection, String separator)
  5. toString(Collection elements, String separator)
  6. toString(Collection objects, CharSequence delimiter)
  7. toString(Collection bytes, Collection result)
  8. toString(Collection collection)
  9. toString(Collection strings)

  10. HOME | Copyright © www.java2s.com 2016