Java Collection to String toString(Collection collection)

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

Description

to String

License

Open Source License

Declaration

public static <T extends Object> String toString(Collection<T> collection) 

Method Source Code

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

import java.util.Collection;

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

        if (collection.size() == 0) {
            return "";
        }/*from   w  w  w  .j a va 2 s.  c o  m*/

        for (T object : collection) {
            sb.append(object.toString());
            sb.append(",");
        }

        return sb.substring(0, sb.length() - 1);
    }

    public static <T extends Object> String toString(Collection<T> collection, String delim) {
        StringBuilder sb = new StringBuilder();

        if (collection.size() == 0) {
            return "";
        }

        for (T object : collection) {
            sb.append(object.toString());
            sb.append(delim);
        }

        return sb.substring(0, sb.length() - 1);
    }
}

Related

  1. toString(Collection objects, CharSequence delimiter)
  2. toString(Collection bytes, Collection result)
  3. toString(Collection collection)
  4. toString(Collection strings)
  5. toString(Collection a, String begStr, String sepStr, String endStr)
  6. toString(Collection collection)
  7. toString(Collection collection)
  8. toString(Collection collection)
  9. toString(Collection collection, String divider)

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