{"id":3353,"date":"2025-12-24T10:00:48","date_gmt":"2025-12-24T15:00:48","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3353"},"modified":"2025-12-24T10:00:48","modified_gmt":"2025-12-24T15:00:48","slug":"java-teeing-collectors","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/java-teeing-collectors\/","title":{"rendered":"Java Teeing Collectors"},"content":{"rendered":"\n<p>Java 12 introduced a new collector called the Teeing collector. This collector allows you to process elements with two different collectors simultaneously and combine the results into a single output. In this article, we&#8217;ll take a closer look at the Teeing collector and how you can use it in your Java code.<\/p>\n\n\n\n<p>What is the Teeing collector?<\/p>\n\n\n\n<p>The Teeing collector is a specialized form of the Collectors class, which provides a variety of collectors for use with streams. The Teeing collector takes three arguments: two collectors and a merging function. The first collector processes the elements of the stream and produces a result of type T1, while the second collector processes the same elements and produces a result of type T2. The merging function then takes the two results and combines them into a single result of type R.<\/p>\n\n\n\n<p>Here&#8217;s the signature of the Teeing collector:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static &lt;T, T1, T2, R&gt; Collector&lt;T, ?, R&gt; teeing(     Collector&lt;? super T, ?, T1&gt; downstream1,     Collector&lt;? super T, ?, T2&gt; downstream2,     BiFunction&lt;? super T1, ? super T2, ? extends R&gt; merger)\n<\/code><\/pre>\n\n\n\n<p>As you can see, the Teeing collector takes two downstream collectors and a merging function. The first and second collectors represent the two calculations that you want to perform on the stream elements, while the merging function combines the results of the two collectors into a single output.<\/p>\n\n\n\n<p>Using the Teeing collector<\/p>\n\n\n\n<p>Let&#8217;s look at an example to see how the Teeing collector works in practice. Suppose we have a list of integers and we want to calculate the sum and the average of the elements, and then use these values to calculate the sum divided by the average.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);\nint sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));\ndouble average = numbers.stream().collect(Collectors.averagingInt(Integer::intValue));\ndouble result = numbers.stream().collect(\n\tCollectors.teeing( Collectors.summingInt(Integer::intValue),\n\t\tCollectors.averagingInt(Integer::intValue),\n\t\t(sumVal, avgVal) -&gt; sumVal \/ avgVal\n\t)\n);\n<\/code><\/pre>\n\n\n\n<p>In this example, we first use the <code>summingInt<\/code> collector to calculate the sum of the elements, and the <code>averagingInt<\/code> collector to calculate the average of the elements. We then use the Teeing collector to combine these values into a single output by dividing the sum by the average.<\/p>\n\n\n\n<p>Benefits of the Teeing collector<\/p>\n\n\n\n<p>The Teeing collector provides a convenient way to perform two separate calculations on the same data and combine the results into a single output. This can be useful in a variety of scenarios where multiple statistics or calculations need to be performed on the same data.<\/p>\n\n\n\n<p>The Teeing collector is also very easy to use. You simply pass in the two collectors and the merging function, and the collector takes care of the rest. This makes it a great tool for stream processing, where you often want to perform multiple calculations on the same data.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>The Teeing collector is a powerful new feature introduced in Java 12 that allows you to process elements with two different collectors simultaneously and combine the results into a single output. In this article, we&#8217;ve looked at how the Teeing collector works and how you can use it in your Java code. By using the Teeing collector, you can perform multiple calculations on the same data and combine the results into a single output, making your code more efficient and easier to read.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 12 introduced a new collector called the Teeing collector. This collector allows you to process elements with two different collectors simultaneously and combine the results into a single output. In this article, we&#8217;ll take a closer look at the Teeing collector and how you can use it in your Java code. What is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[456],"tags":[],"series":[],"class_list":["post-3353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lambda_stream"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/lost-places-g4dcac2ba2_640.jpg?fit=640%2C427&ssl=1","jetpack-related-posts":[{"id":3340,"url":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/java-collectors\/","url_meta":{"origin":3353,"position":0},"title":"Java Collectors","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 8 introduced the Collectors class, which provides a variety of collectors for use with streams. Collectors are used to accumulate the elements of a stream into a single result, such as a list or a map. In this article, we'll take a closer look at the Collectors class and\u2026","rel":"","context":"In &quot;Lambda's and Streams&quot;","block_context":{"text":"Lambda's and Streams","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lambda_stream\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2404,"url":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/using-collector-interface\/","url_meta":{"origin":3353,"position":1},"title":"Using java.util.stream.Collector","author":"Jeffery Miller","date":"December 18, 2025","format":false,"excerpt":"You will find plenty of articles on how to create your own Collector, and calling Streams.collect() with a collector to gather the data into a collection. Learn how to add a Collector interface to your code in order to make a collection.","rel":"","context":"In &quot;Lambda's and Streams&quot;","block_context":{"text":"Lambda's and Streams","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lambda_stream\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/attorney-2743547_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/attorney-2743547_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/attorney-2743547_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2473,"url":"https:\/\/www.mymiller.name\/wordpress\/pipelines\/java-pipelines\/","url_meta":{"origin":3353,"position":2},"title":"Java Pipelines","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Java streams introduced a new way to program for developers. Have a dataset and build a stream to filter, it maps it, and then collect it in the new form. This gave developers a powerful tool to use in development and data processing. I can't even begin to say how\u2026","rel":"","context":"In &quot;Pipelines&quot;","block_context":{"text":"Pipelines","link":"https:\/\/www.mymiller.name\/wordpress\/category\/pipelines\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/machine-495376_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/machine-495376_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/machine-495376_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3737,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/java-23-is-here-exploring-the-full-release-and-incubator-features\/","url_meta":{"origin":3353,"position":3},"title":"Java 23 is Here: Exploring the Full Release and Incubator Features","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Java 23 arrived in September 2023 with a range of new features and improvements. While it may not be a Long-Term Support (LTS) release, it offers some exciting additions worth exploring. In this blog post, we\u2019ll dive into the full release features of Java 23, providing clear explanations and practical\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/immune-defense-1359197_1280-jpg.avif 3x"},"classes":[]},{"id":3148,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-tips-part-2\/","url_meta":{"origin":3353,"position":4},"title":"Java Tips Part 2","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Continuing my Java Tips from experience that I have learned from code reviews to different programming tasks. Tip 6: Don't call .toString() on slf4j logging calls. So if you're following my other tips and using the formatting option of the logging messages like this: list.parallelStream().filter(item -> !item.contains(\"BOB\")).forEach(item -> logger.debug(\"Item: {}\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3388,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/vector-api-for-computations\/","url_meta":{"origin":3353,"position":5},"title":"Vector API for computations","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 16 introduced a new feature called the Vector API, which provides a set of low-level vector operations for performing mathematical calculations on large sets of data. The Vector API is designed to take advantage of the hardware capabilities of modern CPUs, such as SIMD (Single Instruction Multiple Data) instructions,\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/board-g12f4be736_640.jpg?fit=640%2C424&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3353"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3353\/revisions"}],"predecessor-version":[{"id":3356,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3353\/revisions\/3356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3355"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3353"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}