🎓 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 Stream<Character> back into a String can be achieved using the Stream API. This is particularly useful when you've performed operations on individual characters in a stream and need to reassemble them into a single string. Java provides an efficient way to do this using Collectors.joining() or StringBuilder.
In this guide, we will learn how to convert a stream of characters into a string using Java 8.
Solution Steps
- Create a Stream of Characters: Generate or obtain a
Stream<Character>that you want to convert back to a string. - Convert the Stream to a String: Use
Collectors.joining()orStringBuilderto concatenate the characters back into a string. - Display the Result: Print or use the resulting string.
Java Program
Method 1: Using Collectors.joining()
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamOfCharactersToString {
public static void main(String[] args) {
// Step 1: Define a List of Characters
List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');
// Step 2: Convert the Stream of Characters to a String
String result = charList.stream()
.map(String::valueOf) // Convert each Character to String
.collect(Collectors.joining()); // Join them together
// Step 3: Display the result
System.out.println(result);
}
}
Output
Hello, World!
Explanation
Step 1: Create a Stream of Characters
We first define a list of characters:
List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');
Step 2: Convert the Stream to a String
We use map(String::valueOf) to convert each Character into a String, and then Collectors.joining() to concatenate the characters:
String result = charList.stream()
.map(String::valueOf) // Convert each character to String
.collect(Collectors.joining()); // Join all together
Step 3: Display the Result
Finally, the resulting string is printed:
System.out.println(result);
Method 2: Using StringBuilder
import java.util.Arrays;
import java.util.List;
public class StreamOfCharactersToStringUsingStringBuilder {
public static void main(String[] args) {
// Step 1: Define a List of Characters
List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');
// Step 2: Use StringBuilder to collect the characters into a String
StringBuilder result = new StringBuilder();
charList.stream().forEach(result::append); // Append each character to StringBuilder
// Step 3: Display the result
System.out.println(result.toString());
}
}
Output
Hello, World!
Explanation
Step 1: Create a Stream of Characters
We define the list of characters, similar to Method 1:
List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');
Step 2: Use StringBuilder to Collect Characters
Instead of using Collectors.joining(), we use StringBuilder to efficiently append each character from the stream:
StringBuilder result = new StringBuilder();
charList.stream().forEach(result::append); // Append characters to StringBuilder
Step 3: Display the Result
The resulting string is printed using toString() from StringBuilder:
System.out.println(result.toString());
Conclusion
Converting a Stream<Character> to a String in Java 8 can be easily done using either Collectors.joining() or StringBuilder. Both approaches are effective, with Collectors.joining() being more concise and StringBuilder being more efficient for large collections of characters. Both methods offer flexible ways to handle stream processing and string concatenation in Java 8.
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