join List to String - Java java.util

Java examples for java.util:List Join

Description

join List to String

Demo Code


import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Main{
    public static void main(String[] argv){
        List list = java.util.Arrays.asList("asdf","java2s.com");
        String s = "java2s.com";
        System.out.println(join(list,s));
    }//from   ww w  .ja v a2 s  .com
    public static String join(@NotNull List<String> list, @NotNull String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : list) {
            stringBuilder.append(str);
            stringBuilder.append(s);
        }
        return stringBuilder.toString().substring(0,
                stringBuilder.toString().length() - s.length());
    }
}

Related Tutorials