Join Collection to String by comma - Java java.util

Java examples for java.util:Collection Join

Description

Join Collection to String by comma

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection c = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(toString(c));
    }// ww  w  .  j  a  v a2  s. c o  m

    public static String toString(Collection<String> c) {
        StringBuilder sb = new StringBuilder();
        for (String s : c) {
            sb.append(s).append(",");
        }
        if (sb.toString().endsWith(",")) {
            return sb.substring(0, sb.length() - 1);
        } else {
            return sb.toString();
        }
    }
}

Related Tutorials