Given a list
List l = new ArrayList();
l.add(\"one\");
l.add(\"two\");
l.add(\"three\");
I have a method
<
I like using Guava for this purpose. Neat and very useful:
Joiner.on(",").join(myList)
This kind of code has been written time and time again and you should rather be freed implementing your specific implementation logic.
If you use maven, herewith the dependency:
com.google.guava
guava
28.1-jre
It has a bunch of other wonderful cool features too!
This will produce the string "one, two, and three".
List originalList = Arrays.asList("one", "two", "three");
Joiner.on(", ")
.join(originalList.subList(0, originalList.size() - 1))
.concat(", and ")
.concat(originalList.get(originalList.size() - 1));