Fastest way to put contents of Set<String> to a single String with words separated by a whitespace?

Posted by Lars Andren on Stack Overflow See other posts from Stack Overflow or by Lars Andren
Published on 2010-06-15T01:45:18Z Indexed on 2010/06/15 1:52 UTC
Read the original article Hit count: 311

Filed under:
|
|
|
|

I have a few Set<String>s and want to transform each of these into a single String where each element of the original Set is separated by a whitespace " ". A naive first approach is doing it like this

Set<String> set_1;
Set<String> set_2;

StringBuilder builder = new StringBuilder();
for (String str : set_1) {
  builder.append(str).append(" ");
}

this.string_1 = builder.toString();

builder = new StringBuilder();
for (String str : set_2) {
  builder.append(str).append(" ");
}

this.string_2 = builder.toString();

Can anyone think of a faster, prettier or more efficient way to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about string