Everyone will experience the scenario where we need to join the strings in java .
These are the few ways we can do String concatenation in java 8.
Lets say we have 5 strings we want to make it 1 string with ',' separator .
String finalval = String.join(",", "First", "Second", "Third", "Forth");
output : First,Second,Third,Forth
In Case of array of String then,
String arry[] = { "Mango", "Apple", "Papaya" };
String finalval = String.join("-", arry);
output : Mango-Apple-Papaya
Lets say we want to append string to create IN params for SQL Query then,
import java.util.StringJoiner;
StringJoiner sj = new StringJoiner("','", "('", "')");
sj.setEmptyValue("It is empty");
System.out.println(sj.toString()); //prints 'It is empty' as nothing is there to join
sj.add("nike").add("Reebok").add("Puma").add("LV").add("Gucci");
System.out.println(sj.toString());
output:('nike','Reebok','Puma','LV','Gucci')
We can merge the two StringJoiners,
StringJoiner sj2 = new StringJoiner(",");
sj2.add("Fossil");
sj.merge(sj2);
System.out.println(sj.toString());
output:('nike','Reebok','Puma','LV','Gucci','Fossil')