🎓 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 teeing() method in Java, part of the java.util.stream.Collectors class, is used to combine the results of two different collectors into a single result. This method is useful when you need to perform two different collection operations on the same stream and then merge the results.
Table of Contents
- Introduction
teeing()Method Syntax- Understanding
teeing() - Examples
- Basic Usage
- Using
teeing()with Custom Objects
- Real-World Use Case
- Conclusion
Introduction
The teeing() method returns a Collector that is a composite of two downstream collectors. It processes the input elements with both collectors and then merges their results using a specified merger function.
teeing() Method Syntax
The syntax for the teeing() method is as follows:
public static <T, R1, R2, R> Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)
Parameters:
downstream1: The first downstream collector.downstream2: The second downstream collector.merger: A function that merges the results of the two downstream collectors.
Returns:
- A
Collectorthat combines the results of the two downstream collectors.
Throws:
- This method does not throw any exceptions.
Understanding teeing()
The teeing() method allows you to perform two different collection operations on a stream and then merge their results. This is useful when you need to derive multiple pieces of information from the same stream and combine them into a single result.
Examples
Basic Usage
To demonstrate the basic usage of teeing(), we will create a stream of integers, calculate both the sum and the count of the elements, and then combine these results into a single string.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.function.BiFunction;
public class TeeingExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Use teeing to calculate both the sum and the count of the elements
String result = numbers.stream()
.collect(Collectors.teeing(
Collectors.summingInt(Integer::intValue),
Collectors.counting(),
(sum, count) -> "Sum: " + sum + ", Count: " + count
));
System.out.println(result);
}
}
Output:
Sum: 15, Count: 5
Using teeing() with Custom Objects
This example shows how to use teeing() with a stream of custom objects to calculate the total price and the number of products, and then combine these results into a single string.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class TeeingCustomObjectExample {
static class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
double getPrice() {
return price;
}
}
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product("Product A", 10.0),
new Product("Product B", 20.5),
new Product("Product C", 15.8),
new Product("Product D", 30.2),
new Product("Product E", 25.0)
);
// Use teeing to calculate both the total price and the number of products
String result = products.stream()
.collect(Collectors.teeing(
Collectors.summingDouble(Product::getPrice),
Collectors.counting(),
(totalPrice, count) -> "Total Price: " + totalPrice + ", Count: " + count
));
System.out.println(result);
}
}
Output:
Total Price: 101.5, Count: 5
Real-World Use Case
Combining Average and Total Calculation
In real-world applications, the teeing() method can be used to combine the calculation of the average and total of a numeric property from a list of objects.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AverageAndTotalExample {
static class Sale {
double amount;
Sale(double amount) {
this.amount = amount;
}
double getAmount() {
return amount;
}
}
public static void main(String[] args) {
List<Sale> sales = Arrays.asList(
new Sale(100.0),
new Sale(200.0),
new Sale(150.0),
new Sale(50.0),
new Sale(300.0)
);
// Use teeing to calculate both the average and the total sales amount
String result = sales.stream()
.collect(Collectors.teeing(
Collectors.averagingDouble(Sale::getAmount),
Collectors.summingDouble(Sale::getAmount),
(average, total) -> "Average: " + average + ", Total: " + total
));
System.out.println(result);
}
}
Output:
Average: 160.0, Total: 800.0
Conclusion
The Collectors.teeing() method is used to combine the results of two different collectors into a single result. This method is particularly useful for performing multiple collection operations on a stream and merging their results. By understanding and using this method, you can efficiently manage complex collection operations 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