Convert a String to a List of Characters in Java

JavaObject Oriented ProgrammingProgramming

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, !]
raja
Published on 26-Sep-2019 12:57:59
Advertisements