🎓 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
1. Introduction
This tutorial covers the map() method in the Java Stream API. The map() is an intermediate operation that transforms each element in a stream using a provided function. This method is key for data transformation tasks, allowing each element in the stream to be mapped to a new form.
Key Points
1. map() applies a function to each element of a stream, transforming them into a new form based on the function's logic.
2. It returns a new stream consisting of the results of applying the function to the elements of the original stream.
3. This method is commonly used for converting data types, changing the structure of elements, or extracting information from complex objects.
2. Program Steps
1. Create a Stream of elements.
2. Apply the map() method to transform these elements.
3. Collect or process the results to demonstrate the transformations.
3. Code Program
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamMapExample {
public static void main(String[] args) {
// Stream of numbers
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);
// Transform numbers into their squares
List<Integer> squares = numberStream.map(n -> n * n).collect(Collectors.toList());
System.out.println("Squares: " + squares);
// Stream of words
Stream<String> wordsStream = Stream.of("hello", "world", "java", "stream");
// Transform words into uppercase
List<String> uppercaseWords = wordsStream.map(String::toUpperCase).collect(Collectors.toList());
System.out.println("Uppercase Words: " + uppercaseWords);
}
}
Output:
Squares: [1, 4, 9, 16, 25] Uppercase Words: [HELLO, WORLD, JAVA, STREAM]
Explanation:
1. Stream.of(1, 2, 3, 4, 5) creates a stream of integers.
2. numberStream.map(n -> n * n) applies a function to each integer to compute its square and returns a new stream of these squares.
3. squares.collect(Collectors.toList()) collects the results into a list and prints the squares of the original numbers.
4. Stream.of("hello", "world", "java", "stream") creates a stream of strings.
5. wordsStream.map(String::toUpperCase) applies the String.toUpperCase method reference to transform each word into uppercase.
6. uppercaseWords.collect(Collectors.toList()) collects the transformed words into a list and prints them.
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