🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java 8, converting a String to a List<Character> is easy using the Stream API. This approach is useful when you need to manipulate or process individual characters in a string. By converting a string into a stream of characters, you can then collect the characters into a list and perform further operations as needed.
In this guide, we will learn how to convert a String to a List<Character> using Java 8.
Solution Steps
- Define the Input String: Create a string that will be converted into a list of characters.
- Convert the String to an IntStream: Use the
chars()method to convert the string into anIntStreamof character codes. - Map Character Codes to Characters: Use
mapToObj()to convert theIntStreamof character codes toCharacterobjects. - Collect the Stream to a List: Use
collect(Collectors.toList())to collect the characters into aList<Character>.
Java Program
import java.util.List;
import java.util.stream.Collectors;
public class StringToListOfCharacters {
public static void main(String[] args) {
// Step 1: Define the input string
String input = "Hello, World!";
// Step 2-4: Convert the string to a list of characters using Stream API
List<Character> charList = input.chars() // Convert string to IntStream
.mapToObj(c -> (char) c) // Map each int to Character
.collect(Collectors.toList()); // Collect to List
// Display the result
System.out.println(charList);
}
}
Output
[H, e, l, l, o, ,, , W, o, r, l, d, !]
Explanation
Step 1: Define the Input String
We start by defining a string that we want to convert into a list of characters:
String input = "Hello, World!";
Step 2: Convert the String to an IntStream
The chars() method converts the string into an IntStream of character codes. Each element in this stream represents the Unicode value (integer code) of a character from the string:
IntStream charStream = input.chars();
Step 3: Map Character Codes to Characters
We use mapToObj() to map each integer (character code) to a Character object. This allows us to work with the actual characters instead of their Unicode values:
Stream<Character> characterStream = charStream.mapToObj(c -> (char) c);
Here, c -> (char) c converts each integer back to a char, and the Stream<Character> is created.
Step 4: Collect the Stream to a List
Finally, we collect the stream of characters into a List<Character> using Collectors.toList():
List<Character> charList = characterStream.collect(Collectors.toList());
This step gathers the individual characters into a list, which can be used for further processing or manipulation.
Conclusion
Using Java 8's Stream API, converting a String to a List<Character> is both simple and efficient. By leveraging the chars() method, mapToObj(), and collect(), you can easily process each character in a string and collect them into a list for further operations.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment