🎓 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
The of() method in Java, part of the java.util.stream.DoubleStream interface, is used to create a DoubleStream from specified double values. This method is useful when you need to quickly create a DoubleStream with known elements.
Table of Contents
- Introduction
of()Method Syntax- Understanding
of() - Examples
- Basic Usage
- Creating a Stream from an Array of Doubles
- Real-World Use Case
- Conclusion
Introduction
The of() method is a static method that allows you to create a DoubleStream containing the specified double values. This method provides a convenient way to create a stream from a fixed set of elements.
of() Method Syntax
The syntax for the of() method is as follows:
static DoubleStream of(double... values)
Parameters:
values: A varargs parameter representing the double values to be included in the stream.
Returns:
- A
DoubleStreamcontaining the specified double values.
Throws:
- This method does not throw any exceptions.
Understanding of()
The of() method is a straightforward way to create a DoubleStream with known elements. It can take any number of double values as arguments and returns a stream containing those values.
Examples
Basic Usage
To demonstrate the basic usage of of(), we will create a DoubleStream with a few double values and print them.
Example
import java.util.stream.DoubleStream;
public class OfExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Print the elements of the stream
doubleStream.forEach(System.out::println);
}
}
Output:
1.1
2.2
3.3
4.4
5.5
Creating a Stream from an Array of Doubles
This example shows how to create a DoubleStream from an array of doubles using the of() method.
Example
import java.util.stream.DoubleStream;
public class ArrayExample {
public static void main(String[] args) {
double[] values = {6.6, 7.7, 8.8, 9.9, 10.10};
// Create a DoubleStream from an array of doubles
DoubleStream doubleStream = DoubleStream.of(values);
// Print the elements of the stream
doubleStream.forEach(System.out::println);
}
}
Output:
6.6
7.7
8.8
9.9
10.1
Real-World Use Case
Initializing Sensor Data Stream
In real-world applications, the of() method can be used to initialize a stream of sensor data for processing.
Example
import java.util.stream.DoubleStream;
public class SensorDataExample {
public static void main(String[] args) {
// Initialize sensor data stream
DoubleStream sensorData = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);
// Process the sensor data stream
sensorData.forEach(reading -> System.out.println("Sensor reading: " + reading));
}
}
Output:
Sensor reading: 25.3
Sensor reading: 26.7
Sensor reading: 24.8
Sensor reading: 27.5
Sensor reading: 30.1
Conclusion
The DoubleStream.of() method is used to create a DoubleStream from specified double values. This method is particularly useful for quickly creating streams with known elements. By understanding and using this method, you can efficiently initialize and manipulate streams of double values in your Java applications.
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