Join List Of Strings in Java

Join List Of Strings in Java | In this post, we will discuss how to join the list of strings in the Java programming language. There are many ways using which we can traverse a list of strings and join them into a single string. To join strings in Java or append a string in Java, we can take the help of the String class concat() method, concatenation operator, StringBuilder, or StringBuffer.

Example to Join List of Strings in Java:-
Example-1:-
List: [Pro, gram, ming]
Final string after joining the list of strings: “Programming”

Example-2:- When we need to add some character/string while joining the list of strings.
List: [Hi, how, are, you?]
String: “Hi how are you?”

Program to Join List of Strings in Java using Concatenation Operator

In Java “+” is known as the concatenation operator and can be used to add numbers or concatenate string literals and objects. The below program shows how to join the list of strings in Java using the concatenation operator.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Pro");
        list.add("gram");
        list.add("ming");
        System.out.println("List: " + list);

        String string = "";
        for (String str : list) {
            string += str;
        }
        System.out.println("String: " + string);
    }
}

Output:-

List: [Pro, gram, ming]
String: Programming

In the above program, we have to take a list of strings and iterated through it using the for-each loop. We had taken a string to store the final result and initialized it with an empty string (""). In each iteration, we concatenate the current string.

Program to Join List of Strings in Java using concat() Method

We can solve the same task using the String class concat() method. The concat() method of the String class works very similarly to the concatenation operator. The below program shows how to join the list of strings in Java using concat() method.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Pro");
        list.add("gram");
        list.add("ming");
        System.out.println("List: " + list);

        String string = "";
        for (String str : list) {
            string = string.concat(str);
        }
        System.out.println("String: " + string);
    }
}

Output:-

List: [Pro, gram, ming]
String: Programming

Join List of Strings in Java using StringBuilder

As we know string is immutable in Java, see more:- Immutable Class in Java. It means once a string object is created we can’t modify them. After every modification, a new string object will be created.

In above both programs, after every concatenation operation, a new string object is created. If the size of the list is 3 then 3 new string is created which is not useful. If the size of the list is larger then it will create a huge number of unnecessary string objects, will consume memory, and reduce the performance of the program. Therefore it is better to use StringBuilder or StringBuffer class. Also see:- When To Use StringBuffer And StringBuilder In Java.

StringBuilder and StringBuffer classes are mutable objects and we can perform modifications on the same object without creating a new object. For this first, we have to join the list of strings to StringBuilder using the append() method and later convert StringBuilder to String.

Let us demonstrate through an example how to join the list of strings in Java using the StringBuilder object.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Pro");
        list.add("gram");
        list.add("ming");
        System.out.println("List: " + list);

        StringBuilder sb = new StringBuilder("");
        for (String string : list) {
            sb.append(string);
        }
        String string = sb.toString();
        System.out.println("String: " + string);
    }
}

Output:-

List: [Pro, gram, ming]
String: Programming

Join List of Strings in Java using join()

If we have a list of strings representing a sentence and we want to join them by separating strings by space then we can take the help of the String class join() method.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Hi");
        list.add("how");
        list.add("are");
        list.add("you?");
        System.out.println("List: " + list);

        String string = String.join(" ", list);
        System.out.println("String: " + string);
    }
}

Output:-

List: [Hi, how, are, you?]
String: Hi how are you?

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *