I have below code
List<String> test = new ArrayList<String>();
test.add("one");
test.add("two");
test.add("three");
Need output as "one,two,three"
in a single string using Array Utils. Need a single line solution.
You can't do this with ArrayUtils. You can use Apache's StringUtils join function to get the result you want.
// result is "one,two,three"
StringUtils.join(test, ',');
If you don't want to use a library, you can create this function:
public static String joiner(List<String> list, String separator){
StringBuilder result = new StringBuilder();
for(String term : list) result.append(term + separator);
return result.deleteCharAt(result.length()-separator.length()).toString();
}
Use join
String joined2 = String.join(",", test );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With