Array to String Conversion in Java

In Java programming language, an array is a data structure that stores the same type of value in a contiguous memory location. These values can be accessed by using the index of respective values. Whereas a string is an object that stores a sequence of characters. In some cases, we required to convert an array to string. In order to perform the same Java provides methods like toString(), StringBuilder.append(), etc. Using these methods, we can convert an array to a string object.

How to convert array to String in Java?

Consider the following scenario.

If we have an array (say stuname[]) of size two that contains the firstname and lastname of a student. We want to convert this array into a single string, in such a case, we can use the following methods to convert array into a string.

Using Arrays.toString() Method:

The Arrays.toString() method is widely used methods to convert an array to a string. In the Arrays.toString() method, we pass the name of the array that we want to convert to string. In result, the method returns the content of the array in string form. The values are separated using comma(,) and all these values are enclosed using a square bracket([]).

Let's see a Java program that converts an array to string.

ArrayToString.java

Output:

Boolean Array: [true, false, false, true]
Character Array: [a, e, i, o, u]
Double Array: [1.0, 2.0, 3.0, 4.0, 5.0]
Integer Array: [1, 2, 3, 4, 5]
Object Array: [1, 2, 3, 4, 5]

The space and time complexity of this method is O(n), where n is the length of the final string.

Using StringBuilder.append(char[])

We can also use StringBuilder.append() method to convert the array to string. The method java.lang.StringBuilder.append(char[] arr) is the built-in method that takes an array as an argument and converts the array representation to a string.

ArrayToString.java

Output:

Vowels
The result after appending = Vowels AEIOU

The space and time complexity of this method is O(n), where n is the length of the final string.

Java Streams API

We can use Java stream API to convert an array to a string. In Java 8 and above versions of Java we have String.join() method that creates a new string by joining elements and separating them with a specified delimiter.

Note:

  • The Java stream API is used to operate on collections of objects. The stream is a series of objects that support different methods(like map, filter, etc) that can be used to get the desired results.
  • A delimiter is a character or a set of characters that define the start or end of separate elements. The character is used when joining the array elements to create a string, in our example, the delimiter used is an empty string (""). So, when the elements of the array are joined together using the Joiner.join() method the elements will be separated using an empty string. Here, in this case we use an empty string as the delimiter.

ArrayToString.java

Output:

ConvertedusingJavaStreams

The space and time complexity of this method is O(n), where n is the length of the final string.

Using StringUtils.join():

We can use StringUtils.join() method of the StringUtils class from the Apache Commons Lang library to convert an array to a string. It takes an array, joins the array's elements together, and returns a string.

ArrayToString.java

Output:

This is a tree

The space and time complexity of this method is O(n), where n is the length of the final string.

Using Joiner.join() Method:

We can convert an array to a string in java using the joiner.join() method which is provided by the Guava library, it is very simple to use, we can also add a delimiter, in the below example, the delimiter is an empty string.

A delimiter is a character or a set of characters that define the start or end of separate elements. This character is used when joining the array elements to create a string, in our example, the delimiter used is an empty string(""). So when the elements of the array are joined together using the Joiner.join() method the elements will be separated using an empty string.

ArrayToString.java

Output:

import com.google.common.base.Joiner;
public class ArrayToString {
  public static void main(String[] args) {
    // Initialize an array of strings
    String[] arr = { "Apple", "Mango", "Banana", "Orange" };
    // Use the Joiner.on() method from the Guava library 
    // to join the array of strings into a single string
    String str = Joiner.on("").join(arr);
    // Print the result
    System.out.println(str);
  }
}

The space and time complexity of this method is O(n), where n is the length of the final string.






Latest Courses