The first approach is great, but if you’re working with streams and want more flexibility, Collectors.joining() is a solid alternative:
import java.util.Arrays;
import java.util.stream.Collectors;
String[] words = {"a", "b", "c"};
String result = Arrays.stream(words)
.collect(Collectors.joining(","));
System.out.println(result); // Output: a,b,c
Why use this?
If you need extra formatting (like adding a prefix or suffix), Collectors.joining(“,”, “[”, “]”) is useful. This approach works well for lists, too, not just arrays!