Convert collection As String - Java java.util

Java examples for java.util:Collection Convert

Description

Convert collection As String

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out.println(collectionAsString(collection));
    }//from   www  .j  ava  2 s . co m

    public static String collectionAsString(Collection<?> collection) {
        if (collection == null)
            return "[null]";
        if (collection.size() == 0)
            return "[empty]";
        StringBuilder builder = new StringBuilder();
        for (Object obj : collection) {
            builder.append(obj).append(", ");
        }
        builder.deleteCharAt(builder.length() - 1);
        return builder.toString();
    }
}

Related Tutorials