Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 566
Lars Andren Avatar asked Jun 15 '10 01:06

Lars Andren


2 Answers

With commons/lang you can do this using StringUtils.join:

String str_1 = StringUtils.join(set_1, " "); 

You can't really beat that for brevity.

Update:

Re-reading this answer, I would prefer the other answer regarding Guava's Joiner now. In fact, these days I don't go near apache commons.

Another Update:

Java 8 introduced the method String.join()

String joined = String.join(",", set); 

While this isn't as flexible as the Guava version, it's handy when you don't have the Guava library on your classpath.

like image 152
Sean Patrick Floyd Avatar answered Sep 18 '22 15:09

Sean Patrick Floyd


If you are using Java 8, you can use the native

String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

method:

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. For example:

 Set<String> strings = 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);
like image 31
Jasper de Vries Avatar answered Sep 19 '22 15:09

Jasper de Vries