Write code to merge Collection to String - Java java.util

Java examples for java.util:Collection Join

Requirements

Write code to merge Collection to String

Demo Code

import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;

public class Main{
    public static void main(String[] argv){
        Collection strings = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(merge(strings));
    }/*from   w  w w  . jav  a2 s . c  o  m*/
    public static String merge(final Collection<String> strings,
            final String separator) {
        final StringBuilder sb = new StringBuilder();
        String localSeparator = "";
        for (final String s : strings) {
            sb.append(localSeparator).append(s);
            localSeparator = separator;
        }
        return sb.toString();
    }
    public static String merge(final Collection<String> strings) {
        return merge(strings, "");
    }
}

Related Tutorials