🎓 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 collect() method in Java, part of the java.util.stream.DoubleStream interface, is a terminal operation that allows you to transform the elements of a stream into a different form, such as a collection. This method is highly versatile and can be used to perform mutable reduction operations on the elements of the stream.
Table of Contents
- Introduction
collect()Method Syntax- Understanding
collect() - Examples
- Basic Usage with
BoxedStream - Using
collect()withDoubleSummaryStatistics
- Basic Usage with
- Real-World Use Case
- Conclusion
Introduction
The collect() method is a terminal operation that performs a mutable reduction operation on the elements of a DoubleStream using a Collector. This method is highly versatile and can be used to collect elements into various collections or to perform custom reduction operations.
collect() Method Syntax
The syntax for the collect() method is as follows:
<R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner)
Parameters:
supplier: A function that provides a new result container.accumulator: A function that folds an element into a result container.combiner: A function that combines two result containers.
Returns:
- The result of the collection operation.
Throws:
- This method does not throw any exceptions.
Understanding collect()
The collect() method allows you to transform the elements of a DoubleStream into a different form. The method takes three arguments:
- A
supplierthat provides a new result container. - An
accumulatorthat folds elements into the result container. - A
combinerthat combines two result containers, typically used in parallel processing.
Examples
Basic Usage with BoxedStream
To demonstrate the basic usage of collect(), we will convert a DoubleStream into a List<Double> using a BoxedStream.
Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.DoubleStream;
public class CollectToListExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Collect the elements of the DoubleStream into a List<Double>
List<Double> doubleList = doubleStream.collect(
ArrayList::new,
ArrayList::add,
ArrayList::addAll
);
System.out.println("Collected List: " + doubleList);
}
}
Output:
Collected List: [1.1, 2.2, 3.3, 4.4, 5.5]
Using collect() with DoubleSummaryStatistics
This example shows how to use collect() to gather statistical information about the elements in a DoubleStream using DoubleSummaryStatistics.
Example
import java.util.DoubleSummaryStatistics;
import java.util.stream.DoubleStream;
public class CollectToStatisticsExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Collect the elements of the DoubleStream into DoubleSummaryStatistics
DoubleSummaryStatistics stats = doubleStream.collect(
DoubleSummaryStatistics::new,
DoubleSummaryStatistics::accept,
DoubleSummaryStatistics::combine
);
System.out.println("Statistics: " + stats);
}
}
Output:
Statistics: DoubleSummaryStatistics{count=5, sum=16.500000, min=1.100000, average=3.300000, max=5.500000}
Real-World Use Case
Collecting Sensor Data into a Custom Collection
In real-world applications, the collect() method can be used to collect sensor data into a custom collection for further analysis.
Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.DoubleStream;
public class SensorDataCollectionExample {
static class SensorData {
private final double value;
SensorData(double value) {
this.value = value;
}
@Override
public String toString() {
return "SensorData{" +
"value=" + value +
'}';
}
}
public static void main(String[] args) {
DoubleStream sensorValues = DoubleStream.of(25.3, 26.7, 23.8, 24.1, 25.9);
// Collect the sensor values into a List<SensorData>
List<SensorData> sensorDataList = sensorValues.collect(
ArrayList::new,
(list, value) -> list.add(new SensorData(value)),
ArrayList::addAll
);
System.out.println("Collected Sensor Data: " + sensorDataList);
}
}
Output:
Collected Sensor Data: [SensorData{value=25.3}, SensorData{value=26.7}, SensorData{value=23.8}, SensorData{value=24.1}, SensorData{value=25.9}]
Conclusion
The DoubleStream.collect() method is used to perform a mutable reduction operation on the elements of a stream. This method is highly versatile and can be used to transform the elements into different forms, such as collections or custom data structures. By understanding and using this method, you can efficiently manage 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