1

Assume i have List Collection of Strings and i want to loop by this List and add each element of List to some variable of type String.

List<String> words;
//assume words have 5 elements
String summary;//variable where i want to keep all elements

for (String word : words){
    //here i want to add new word to the variable summary
}

As i know java always creates new object of String. Even if i try to change value - new object will be created anyway, am i right?

So here is a question how to join all elements of List in one variable?

2

10 Answers 10

3

On any version of Java: Apache Commons has a class StringUtils that has a join method:

String result = StringUtils.join(words, ",")

On Java 8, you can do this natively. See this article.

Sign up to request clarification or add additional context in comments.

Comments

1

use a StringBuilder to join all the words.

List<String> words;
//assume words have 5 elements
StringBuilder summary = new StringBuilder();

for (String word : words){
    summary.append(word);
}

Finally, get the joined String with summary.toString().

Note : If you have an idea of the number of characters that would be appended to the StringBuilder, it will be more efficient to use the constructor that gets an initial size :

summary = new StringBuilder(size);.

Comments

1

I think the easiest solution would be to use a StringBuilder:

String summary;
List<String> words = ...;
StringBuilder builder = new StringBuilder();
for (String word : words) {
    builder.append(word);
}

summary = builder.toString();

Comments

1

The simplest way would be to use an existing functionality, for example Apache Common's StringUtils.join(). If that's not possible, this will work:

    StringBuilder sb = new StringBuilder();
    for (String word : words) {
        sb.append(word);
    }
    String summary = sb.toString();

Comments

1

You can try the following code,

List<String> words;
String summary = null;

for (String word : words)
{
    summary = summary + word + " , ";
}
System.out.println("List items : " + summary);

1 Comment

Inefficient due to repeated creations of a String object.
1

An alternative to Apache commons (StringUtils) is Guava's Joiner.

For example:

 List<String> words = new ArrayList<>();
 words.add("word");
 words.add("anotherWord");

 String joinedWords = Joiner.on(",").join(words);

This may also be useful if you're not able to use Java 8.

See wiki: https://github.com/google/guava/wiki/StringsExplained

Comments

0

As Eran suggest or just, use simple concatination

List<String> words;
//assume words have 5 elements
String summary = "";//variable where i want to keep all elements

for (String word : words){
    summary = summary + word;
}

1 Comment

This is terribly inefficient, it'll create intermediate strings and will end up being O(n^2) (see: Schlemiel the Painter's algorithm). Use a StringBuilder instead, as shown in my answer.
0

Have you looked at

StringUtils.join()

as a possible solution?

Comments

0
String summary = words.stream().collect( Collectors.joining() );

Comments

-1

You can simply add.

summary += word;

1 Comment

summary is initially null: you can't do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.