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
Collector
that 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.
Comments
Post a Comment
Leave Comment