🎓 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, DoubleStream is a sequence of primitive double elements supporting sequential and parallel aggregate operations. It is part of the java.util.stream package and provides a convenient way to work with sequences of double values.
Table of Contents
- What is
DoubleStream? - Creating a
DoubleStream - Common Operations
- Examples of
DoubleStream - Real-World Use Case
- Conclusion
1. What is DoubleStream?
DoubleStream is a specialized stream for working with double values, providing methods for processing elements, performing calculations, and generating statistics.
2. Creating a DoubleStream
You can create a DoubleStream in several ways:
- From an array:
DoubleStream.of(double... values) - Using
Arrays.stream(double[] array) - From a collection using
stream().mapToDouble() - Using
DoubleStream.generate()orDoubleStream.iterate()
3. Common Operations
Some common operations on DoubleStream include:
filter(): Filters elements based on a condition.map(): Transforms elements.average(): Calculates the average of the elements.sum(): Computes the sum of the elements.min()/max(): Finds the minimum or maximum element.sorted(): Sorts the elements.forEach(): Performs an action for each element.
4. Examples of DoubleStream
Example 1: Creating and Summing a DoubleStream
This example demonstrates how to create a DoubleStream from a set of double values and calculate their sum.
import java.util.stream.DoubleStream;
public class SumExample {
public static void main(String[] args) {
double sum = DoubleStream.of(1.2, 2.5, 3.7)
.sum();
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 7.4
Example 2: Filtering and Averaging
In this example, we filter the stream to include only values greater than 2.0 and then calculate the average of the remaining values.
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class FilterAverageExample {
public static void main(String[] args) {
OptionalDouble average = DoubleStream.of(1.2, 2.5, 3.7, 4.1)
.filter(n -> n > 2.0)
.average();
System.out.println("Average: " + average.orElse(0.0));
}
}
Output:
Average: 3.433333333333333
Example 3: Mapping and Sorting
This example shows how to map each element of a DoubleStream to its square and then sort the resulting values.
import java.util.stream.DoubleStream;
public class MapSortExample {
public static void main(String[] args) {
DoubleStream.of(1.2, 2.5, 3.7)
.map(n -> n * n)
.sorted()
.forEach(System.out::println);
}
}
Output:
1.44
6.25
13.69
5. Real-World Use Case: Calculating Standard Deviation
This example illustrates how to calculate the standard deviation of a set of double values using DoubleStream.
import java.util.stream.DoubleStream;
public class StandardDeviationExample {
public static void main(String[] args) {
double[] numbers = {1.2, 2.5, 3.7, 4.1};
double average = DoubleStream.of(numbers).average().orElse(0.0);
double variance = DoubleStream.of(numbers)
.map(n -> Math.pow(n - average, 2))
.average()
.orElse(0.0);
double stdDev = Math.sqrt(variance);
System.out.println("Standard Deviation: " + stdDev);
}
}
Output:
Standard Deviation: 1.1078230586932
Conclusion
DoubleStream is used in Java for processing sequences of double values. It provides a range of operations for filtering, transforming, and aggregating data, making it particularly useful in numerical and scientific computations. Using DoubleStream can lead to cleaner and more efficient code, especially in data processing contexts.
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