🎓 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
In this quick tutorial, we will learn how to use the flatMapToInt() method of the Java Stream API. flatMapToInt() is used to convert elements of a stream that are collections or arrays of integers (or objects that can be mapped to integers) into an IntStream. This is particularly useful for processing nested structures or combining multiple arrays or collections into a single stream of integers.
Key Points
1. flatMapToInt() maps elements of a stream to IntStream objects and flattens the result into a single IntStream.
2. It is ideal for scenarios where you need to transform nested collections or arrays of integers into a single stream.
3. This method facilitates operations on flattened integer data, such as summing or finding averages.
2. Program Steps
1. Create a Stream containing collections or arrays of integers.
2. Apply flatMapToInt() to transform these into a single IntStream.
3. Perform operations such as sum, average, or other reductions on the IntStream.
3. Code Program
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamFlatMapToIntExample {
public static void main(String[] args) {
// Creating a stream of integer arrays
Stream<int[]> arraysStream = Stream.of(new int[]{1, 2}, new int[]{3, 4}, new int[]{5, 6});
// Using flatMapToInt to convert and flatten the stream into an IntStream
IntStream intStream = arraysStream.flatMapToInt(IntStream::of);
// Calculating the sum of all integers in the stream
int sum = intStream.sum();
System.out.println("Sum of all integers: " + sum);
}
}
Output:
Sum of all integers: 21
Explanation:
1. Stream.of(new int[]{1, 2}, new int[]{3, 4}, new int[]{5, 6}) creates a stream of arrays, each containing a pair of integers.
2. arraysStream.flatMapToInt(IntStream::of) applies the flatMapToInt method, which maps each array into an IntStream of its elements and then flattens these streams into a single IntStream.
3. intStream.sum() computes the sum of all elements in the flattened IntStream, yielding the total sum of all integers in the initial arrays.
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