🎓 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 explores the mapToInt() method in the Java Stream API. mapToInt() is used to convert objects within a stream into integers. This is particularly useful for transforming streams where you need to perform numerical calculations or aggregate operations on integer values.
Key Points
1. mapToInt() converts each element of a stream to an integer using a provided int-producing function.
2. It returns an IntStream, which is a stream of primitive int values, allowing for operations that are more efficient than those on a Stream<Integer>.
3. This method is commonly used for operations like summing, finding min or max, or averaging, which require integer values.
2. Program Steps
1. Create a Stream of elements.
2. Apply mapToInt() to transform these elements into integers.
3. Perform operations on the resulting IntStream.
3. Code Program
import java.util.stream.Stream;
public class StreamMapToIntExample {
public static void main(String[] args) {
// Stream of strings representing numerical values
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
// Converting string values to integers and calculating their sum
int sum = stringStream.mapToInt(Integer::parseInt).sum();
System.out.println("Sum of all numbers: " + sum);
}
}
Output:
Sum of all numbers: 15
Explanation:
1. Stream.of("1", "2", "3", "4", "5") creates a stream of strings. Each string represents a numerical value.
2. stringStream.mapToInt(Integer::parseInt) converts each string in the stream to an integer using Integer.parseInt. This transforms the stream of strings into an IntStream.
3. .sum() is called on the resulting IntStream to calculate the sum of all the integers. The method efficiently handles the arithmetic operation on the stream of primitive int values.
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