🎓 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 IntStream.of() method in Java, part of the java.util.stream.IntStream interface, is used to create an IntStream from specified values. This method is overloaded to support creating a stream from a single int or from an array of ints.
Table of Contents
- Introduction
of()Method Syntax- Understanding
of() - Examples
- Basic Usage with Single Value
- Basic Usage with Multiple Values
- Real-World Use Case
- Conclusion
Introduction
The of() method is a static method that creates an IntStream from the specified values. This method is particularly useful when you need to quickly create a stream from a known set of values.
of() Method Syntax
There are two overloads of the of() method:
- Creating a stream from a single int value:
static IntStream of(int t)
- Creating a stream from an array of int values:
static IntStream of(int... values)
Parameters:
t: A single int value.values: An array of int values (varargs).
Returns:
- An
IntStreamcontaining the specified values.
Throws:
- This method does not throw any exceptions.
Understanding of()
The of() method provides a simple and concise way to create an IntStream from specified values. This can be useful for testing, quick data manipulations, and scenarios where the stream values are known beforehand.
Examples
Basic Usage with Single Value
To demonstrate the usage of of() with a single value, we will create an IntStream from a single int value and print it.
Example
import java.util.stream.IntStream;
public class OfSingleValueExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(5);
// Print the elements of the stream
intStream.forEach(System.out::println);
}
}
Output:
5
Basic Usage with Multiple Values
To demonstrate the usage of of() with multiple values, we will create an IntStream from an array of int values and print them.
Example
import java.util.stream.IntStream;
public class OfMultipleValuesExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
// Print the elements of the stream
intStream.forEach(System.out::println);
}
}
Output:
1
2
3
4
5
Real-World Use Case
Creating a Stream for Testing
In real-world applications, the of() method can be used to create a stream for testing purposes. This allows developers to easily create streams with known values for unit tests and other testing scenarios.
Example
import java.util.stream.IntStream;
public class TestingExample {
public static void main(String[] args) {
IntStream testStream = IntStream.of(10, 20, 30, 40, 50);
// Perform a test operation on the stream
int sum = testStream.sum();
// Print the result
System.out.println("Sum of test stream: " + sum);
}
}
Output:
Sum of test stream: 150
Conclusion
The IntStream.of() method is used to create an IntStream from specified values, either a single int or an array of ints. This method is particularly useful for quickly creating streams with known values. By understanding and using this method, you can efficiently create and work with streams of integer 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