🎓 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, the Stream API allows you to efficiently process collections and sequences of elements. Sometimes, after processing a stream, you might want to convert it into a String, either by joining the elements with a delimiter or concatenating them directly. Java 8 provides several ways to achieve this using the Collectors.joining() method.
This guide will demonstrate how to convert a stream to a string in Java 8.
Solution Steps
- Create or Obtain a Stream: Generate or obtain a stream from a collection or another data source.
- Use
Collectors.joining(): Convert the stream into a string by joining the elements with or without a delimiter. - Display or Use the String: Print or use the resulting string.
Java Program
Example 1: Convert a Stream of Strings to a Single String (Without Delimiter)
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class StreamToStringExample {
public static void main(String[] args) {
// Step 1: Create a stream of strings
Stream<String> wordStream = Stream.of("Java", "is", "fun");
// Step 2: Convert the stream to a string without a delimiter
String result = wordStream.collect(Collectors.joining());
// Step 3: Display the result
System.out.println(result); // Output: "Javaisfun"
}
}
Output
Javaisfun
Explanation
- Step 1: A stream of strings is created using
Stream.of(). - Step 2: The
Collectors.joining()method is used to concatenate the elements without any delimiter. - Step 3: The resulting string is printed.
Example 2: Convert a Stream of Strings to a Single String (With a Space Delimiter)
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class StreamToStringWithDelimiter {
public static void main(String[] args) {
// Step 1: Create a stream of strings
Stream<String> wordStream = Stream.of("Java", "is", "fun");
// Step 2: Convert the stream to a string with a space delimiter
String result = wordStream.collect(Collectors.joining(" "));
// Step 3: Display the result
System.out.println(result); // Output: "Java is fun"
}
}
Output
Java is fun
Explanation
- Step 1: A stream of strings is created using
Stream.of(). - Step 2: The
Collectors.joining(" ")method is used to concatenate the elements with a space delimiter between them. - Step 3: The resulting string is printed.
Example 3: Convert a Stream of Integers to a String
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class StreamOfIntegersToString {
public static void main(String[] args) {
// Step 1: Create a stream of integers
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);
// Step 2: Convert the stream of integers to a string
String result = numberStream.map(String::valueOf)
.collect(Collectors.joining(", "));
// Step 3: Display the result
System.out.println(result); // Output: "1, 2, 3, 4, 5"
}
}
Output
1, 2, 3, 4, 5
Explanation
- Step 1: A stream of integers is created using
Stream.of(). - Step 2: The integers are converted to strings using
map(String::valueOf)and then concatenated usingCollectors.joining(", "). - Step 3: The resulting string is printed.
Example 4: Convert a Stream of Custom Objects to a String
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class StreamOfObjectsToString {
public static void main(String[] args) {
// Step 1: Create a stream of Employee objects
Stream<Employee> employeeStream = Stream.of(
new Employee("Ravi", 30),
new Employee("Amit", 25),
new Employee("Pooja", 35)
);
// Step 2: Convert the stream of Employee objects to a string
String result = employeeStream.map(Employee::toString)
.collect(Collectors.joining(", "));
// Step 3: Display the result
System.out.println(result);
}
}
class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + ": " + age;
}
}
Output
Ravi: 30, Amit: 25, Pooja: 35
Explanation
- Step 1: A stream of
Employeeobjects is created usingStream.of(). - Step 2: Each
Employeeobject is converted to a string usingmap(Employee::toString)and then concatenated usingCollectors.joining(", "). - Step 3: The resulting string is printed.
Conclusion
In Java 8, converting a stream to a string is simple using the Collectors.joining() method. You can join the elements of the stream with or without a delimiter, and the process works for strings, numbers, or even custom objects. This approach is flexible and efficient for handling various data types.
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