Java Collection to String toString(Collection collection, String divider)

Here you can find the source of toString(Collection collection, String divider)

Description

to String

License

Open Source License

Declaration

public static <T> String toString(Collection<T> collection, String divider) 

Method Source Code

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

import java.util.Collection;

import java.util.Map;

public class Main {
    public static <T> String toString(Collection<T> collection, String divider) {
        StringBuilder sb = new StringBuilder();

        boolean first = true;

        for (T object : collection) {
            if (first == false) {
                sb.append(divider);//from   w w w.  j  av  a  2  s  . c o  m
            }
            first = false;
            sb.append(toString(object, divider));
        }
        return sb.toString();
    }

    public static <T, S> String toString(Map<T, S> map, String divider) {
        StringBuilder sb = new StringBuilder();

        boolean first = true;

        for (Map.Entry<T, S> entry : map.entrySet()) {
            if (first == false) {
                sb.append(divider);
            }
            first = false;
            sb.append(toString(entry.getKey(), divider)).append(" => ").append(toString(entry.getValue(), divider));
        }

        return sb.toString();
    }

    public static String toString(Object object, String divider) {
        if (object == null) {
            return null;
        } else if (object instanceof Collection) {
            Collection collection = (Collection) object;
            return toString(collection, divider);
        } else if (object instanceof Map) {
            Map map = (Map) object;
            return toString(map, divider);
        } else {
            return object.toString();
        }
    }
}

Related

  1. toString(Collection a, String begStr, String sepStr, String endStr)
  2. toString(Collection collection)
  3. toString(Collection collection)
  4. toString(Collection collection)
  5. toString(Collection collection)
  6. toString(Collection list, String delimeter)
  7. toString(final Collection collection)
  8. toString(final Collection collection)
  9. toString(final Collection collection, final String separator)