Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Convert List of Characters to String in Java



In this article, we will learn to convert a list of characters to strings in Java. Sometimes, when we handle character-based data structures, we need to convert a list of characters into a string.

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

Methods to Convert a List of Characters to a String

The following are the different approaches to convert 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, and Collectors.joining() is used to concatenate them. The 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);
   }
}

Following is the output of the above program -

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. The 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 a 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<Character> list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');
      StringBuilder sb = new StringBuilder();
      
      for (Character 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).

Using a For Loop

We can also use a simple for loop to iterate through the list of characters and concatenate them into a string. The following are the steps to convert a list of characters to a string using a for loop -

  • Initialize an empty string.
  • Use a for loop to iterate through the list of characters.
  • Next, concatenate each character to the string.
  • Print the final string.

This method is similar to the StringBuilder approach but less efficient due to string immutability in Java. Each concatenation creates a new string object. Let's write the code to convert a list of characters to a string using a for loop -

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

public class Example {
   public static void main(String[] args) {
      List<Character> list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');
      String string = "";
      for (Character ch : list) {
         string += ch;
      }
      System.out.println("String = " + string);
   }
}

Output

String = Welcome

Time Complexity: O(N), as each character is concatenated once.
Space Complexity: O(N) (because it creates a new string object for each concatenation).

Using String.valueOf()

The String.valueOf() method is a static method that converts different types of values into their string representation. It can be used to convert characters to strings. The following are the steps to convert a list of characters to a string using String.valueOf() -

  • Initialize an empty string.
  • Use a for loop to iterate through the list of characters.
  • Next, concatenate each character to the string using String.valueOf().
  • Print the final string.

Let's write the code to convert a list of characters to a string using String.valueOf() -

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

public class Example {
   public static void main(String[] args) {
      List<Character> list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e');
      String string = "";
      for (char ch : list) {
         string += String.valueOf(ch);
      }
      System.out.println("String = " + string);
   }
}

Output

String = Welcome
Updated on: 2025-05-06T16:36:36+05:30

811 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements