Java
String Conversion
Programming
Arrays
Coding Tips

Convert array of strings into a string in Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, converting an array of strings into a single string is a common task that can be approached in several ways, each with its own use cases and performance implications. Understanding how to efficiently convert an array of strings into a single string is fundamental for handling text data in Java.

Understanding String Joining in Java

Java does not natively store strings in a manner that allows direct concatenation of array elements into a string because strings in Java are immutable. Each time you concatenate strings, a new string is created. Hence, directly using concatenation in a loop to form a string from an array can be inefficient, particularly with large arrays.

Using String.join()

Introduced in Java 8, String.join() is a convenient method to concatenate an array of String objects in a single line. This method takes the delimiter and an Iterable (e.g., an array or a list) and joins each element of the iterable separated by the delimiter into a single string. Here’s an example:

java
String[] words = {"Hello", "world", "from", "Java"};
String result = String.join(" ", words); // joins with a space as a delimiter
System.out.println(result);  // Output: Hello world from Java

Using StringBuilder

Before Java 8, a common approach to concatenate strings from an array efficiently is using StringBuilder. This class provides an API for mutable sequences of characters, making it ideal for situations where a string needs multiple modifications. Here’s how you can use StringBuilder to convert an array of strings into a single string:

java
1String[] words = {"Hello", "world", "from", "Java"};
2StringBuilder builder = new StringBuilder();
3for (String word : words) {
4    builder.append(word).append(" ");  // Appending each string to the builder
5}
6String result = builder.toString().trim(); // Convert to String and remove the last space
7System.out.println(result);  // Output: Hello world from Java

Using StringBuffer

StringBuffer is similar to StringBuilder, but it is thread-safe. This means that methods are synchronized, and it can be safely used by multiple threads at the same time. However, this comes at a performance cost. Use StringBuffer only if thread safety is required:

java
1String[] words = {"Hello", "world", "from", "Java"};
2StringBuffer buffer = new StringBuffer();
3for (String word : words) {
4    buffer.append(word).append(" ");
5}
6String result = buffer.toString().trim();
7System.out.println(result);  // Output: Hello world from Java

Using Streams

Java 8 introduced Streams, which can also be used to concatenate elements of a string array in a functional style:

java
1String[] words = {"Hello", "world", "from", "Java"};
2String result = Arrays.stream(words)
3                      .collect(Collectors.joining(" ")); // Collectors joining
4System.out.println(result);  // Output: Hello world from Java

Summary Table

Here is a summary table of methods to convert an array of strings into a single string:

MethodDescriptionThread SafeSince Java Version
String.join()Joins elements with a delimiterNo8
StringBuilderMutable sequence of characters for single-thread useNo1.5
StringBufferMutable and synchronized for thread safetyYes1.0
StreamsFunctional-style operations on streams of elementsNo8

Considerations for Choosing a Method

  • Performance: StringBuilder is usually faster for non-threaded applications due to its non-synchronized nature.
  • Ease of use: String.join() and streams offer more compact and readable code.
  • Compatibility: If working with Java versions below 8, StringBuilder or StringBuffer would be the choice.

In practice, choosing the right method depends on the specific requirements of the application, including Java version, performance needs, and code maintainability preferences. For most single-threaded applications where performance is critical, StringBuilder is often the preferred choice. For simpler or less performance-critical code, String.join() or Java Streams provide more readable alternatives.


Course illustration
Course illustration

All Rights Reserved.