Two Ways to Join String in Java 8: StringJoiner and String.join Examples

Joining multiple String literals or objects into one is a common programming requirement, and you will often find situations where you need to convert a list of String or a Collection of String into a CSV String for your application. For a long time, JDK API has no way to join multiple String literals or objects together, which forces programmers to write hacks like looping through all String objects and manually joining them using String concatenation to create the final, joined String. Even though this approach worked, it was filled with errors and hacks; you need to be careful not to add delimiter before the first element and after the last element, which often caused issues, particularly for new Java developers.

But the bigger problem with that approach was that everyone needed to re-invent the wheel. Since it was a very common requirement, you found many programmers writing the same routines and making the same mistakes, often ending in StackOverflow to solve their problems.  Thankfully, Java 8 solved this problem once for all.