Join String list elements with a delimiter in one step [duplicate]

Is there a function like join that returns List's data as a string of all the elements, joined by delimiter provided?

 List<String> join; ....
 String join = list.join('+");
 // join == "Elem 1+Elem 2";

or one must use an iterator to manually glue the elements?


Solution 1:

Java 8...

String joined = String.join("+", list);

Documentation: http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.Iterable-

Solution 2:

You can use the StringUtils.join() method of Apache Commons Lang:

String join = StringUtils.join(joinList, "+");

Solution 3:

Or Joiner from Google Guava.

Joiner joiner = Joiner.on("+");
String join = joiner.join(joinList);