Convert String to List of Characters in Java



Let’s say the following is our string −

String str = "Website!";

Now, convert the above string to a list of characters −

List<Character>list = str.chars().mapToObj(n -> (char)n).collect(Collectors.toList());

Example

Following is the program to convert a string to a list of characters in Java −

 Live Demo

import java.util.*;
import java.util.stream.Collectors;
public class Demo {
   public static void main(String[] args){
      String str = "Website!";
      System.out.println("String = "+str);
      List<Character>list = str.chars().mapToObj(n -> (char)n).collect(Collectors.toList());
      System.out.println("List of Characters = "+list);
   }
}

Output

String = Website!
List of Characters = [W, e, b, s, i, t, e, !]
Updated on: 2019-09-26T12:59:14+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements