Convert List of Characters to String in Java



In this article, we will learn to convert a list of characters to strings in Java. In Java converting a list of characters into a string is a common task, especially when handling character-based data structures.

Problem Statement 

Given a list of characters, the task is to convert it into a single string where the order of characters remains unchanged.

Input ?

list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');

Output ?

Welcome

Different Approaches

The following are the two different approaches to converting a list of characters to a string in Java ?

Using Java Streams

Java Streams provide a functional way to process collections. The map() function is used to convert each character into a string, Collectors.joining() is used to concatenate them.

Following are the steps to convert a list of characters to a string using Java streams ?

  • stream() to convert the list into a stream.
  • map(String::valueOf) converts each character to a string.
  • collect(Collectors.joining()) concatenates all string elements.

Let's say the following is our list of characters ?

List<Character> list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');

Convert the list of characters to a string ?

String string = list.stream().map(String::valueOf).collect(Collectors.joining());

Example

Below is an example of converting a list of characters to a string using Java streams ?

import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List<Character> list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');
      String string = list.stream().map(String::valueOf).collect(Collectors.joining());
      System.out.println("String = "+string);
   }
}

Output

String = Welcome

Time Complexity: O(N), each character is processed once.
Space Complexity: O(N), creates a new string and intermediate stream objects.

Using StringBuilder

The StringBuilder class offers a more efficient way to concatenate characters since it reduces unnecessary memory allocation compared to string concatenation.

Following are the steps to convert a list of characters to a string using the StringBuilder ?

  • A StringBuilder object is initialized.
  • A for-each loop iterates over the list, appending each character.
  • The toString() method converts StringBuilder into a string. 

A StringBuilder object sb is created using the StringBuilder class ?

StringBuilder sb = new StringBuilder();

Converting the StringBuilder object into a string ?

String string = sb.toString();

Example

Below is an example of converting a list of characters to a string using the StringBuilder ?

import java.util.Arrays;
import java.util.List;

public class Demo {
   public static void main(String[] args) {
      List list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');
      StringBuilder sb = new StringBuilder();
      
      for (char ch : list) {
         sb.append(ch);
      }
      
      String string = sb.toString();
      System.out.println("String = " + string);
   }
}

Output

String = Welcome

Time Complexity: O(N), each character is appended once.
Space Complexity: O(1) (modifies the StringBuilder in place, minimizing extra memory allocation).

Conclusion

Both Java Streams and StringBuilder provide efficient ways to convert a list of characters into a string. Streams (Collectors.joining()) offer a functional and concise approach, making the code more readable. StringBuilder is more memory-efficient and performs better, especially for large datasets, as it avoids unnecessary object creation.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-26T19:36:42+05:30

742 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements