comma separated String in java

Convert List to comma-separated String

This Java tutorial shows several ways for converting List to a comma – separated String.

Java 7

public String convertUsingStringBuilder(List<String> names)
{
    StringBuilder namesStr = new StringBuilder();
    for(String name : names)
    {
        namesStr = namesStr.length() > 0 ? namesStr.append(",").append(name) : namesStr.append(name);
    }
    return namesStr.toString();
}

Java 8

Method 1 :

public static String convertUsingJava8(List<String> names)
{
    return String.join(",", names);
}

Method 2 :

public static String convertUsingJava8(List<String> names)
{
    return names.stream().collect(Collectors.joining(","));
}

Apache Commons Lang

1
2
3
4
public static String convertUsingApacheCommons(List<String> names)
{
    return StringUtils.join(names, ',');
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in