I have a few Sets and want to transform each of these into a single String where each element of the original Set is sep
If you are using Java 8, you can use the native
String.join(CharSequence delimiter, Iterable extends CharSequence> elements)
method:
Returns a new
Stringcomposed of copies of theCharSequenceelements joined together with a copy of the specified delimiter. For example:Setstrings = new LinkedHashSet<>(); strings.add("Java"); strings.add("is"); strings.add("very"); strings.add("cool"); String message = String.join("-", strings); //message returned is: "Java-is-very-cool"
Set implements Iterable, so simply use:
String.join(" ", set_1);