Join All Elements from Collection - Java java.util

Java examples for java.util:Collection Join

Description

Join All Elements from Collection

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(mergeAllElements(collection));
    }//  ww  w.  j  a v  a  2 s  .  c o  m

    public static String mergeAllElements(Collection<String> collection) {
        StringBuffer stringBuffer = new StringBuffer();
        Object[] str = collection.toArray();

        for (int i = 0; i < str.length; i++) {
            if (i == str.length - 1) {
                stringBuffer.append(str[i].toString());
            } else {
                stringBuffer.append(str[i].toString()).append(",");
            }
        }

        return stringBuffer.toString();
    }
}

Related Tutorials