{"id":2404,"date":"2025-12-18T10:00:03","date_gmt":"2025-12-18T15:00:03","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=2404"},"modified":"2025-12-18T10:00:03","modified_gmt":"2025-12-18T15:00:03","slug":"using-collector-interface","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/using-collector-interface\/","title":{"rendered":"Using java.util.stream.Collector"},"content":{"rendered":"\n<p>Search for this lovely gem that came in Java 8, and 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. Neither of these was the problem I was trying to solve.\u00a0 I wanted to know how to add this object to my code in order to make a collection.\u00a0 The ability to pass the various types of Collectors <g class=\"gr_ gr_7 gr-alert sel gr_gramm gr_replaced gr_inline_cards gr_disable_anim_appear Grammar replaceWithoutSep\" id=\"7\" data-gr-id=\"7\"><\/g>and make use of them really makes things far more powerful.\u00a0 So let&#8217;s talk about what you need.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Collector object<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.stream.Collector;\nCollector&lt;T, A, R>\tcollector;<\/code><\/pre>\n\n\n\n<p>To begin import in the Collector interface.\u00a0 Then the parameter type or member variable you need is the Collector&lt;T, A, R>.\u00a0 This is the Object you need to keep.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding to the Collection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>A supplier = collector.supplier().get();\ncollector.accumulator().accept(this.supplier, (T) data);<\/code><\/pre>\n\n\n\n<p>First thing get a reference to the supplier.\u00a0 Save this because you will need this later to get the collection when you are finished.<\/p>\n\n\n\n<p>Now access the accumulator to call the accept() method.\u00a0 The first parameter is the supplier, and the second parameter is the data your adding to the Collection.\u00a0 It should be of type T, cast if necessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Getting your final results<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>R collection = collector.finisher().apply(supplier);<\/code><\/pre>\n\n\n\n<p>Once you have added all of your data to your collector.\u00a0 Your final step is to get your Collection from it.\u00a0 This is accomplished by calling apply on the finisher passing in the supplier. This will return to you the type of your collection R.<\/p>\n\n\n\n<p>Hope this helps you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":2405,"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":[69],"series":[],"class_list":["post-2404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lambda_stream","tag-java-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/attorney-2743547_640.jpg?fit=640%2C426&ssl=1","jetpack-related-posts":[{"id":3353,"url":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/java-teeing-collectors\/","url_meta":{"origin":2404,"position":0},"title":"Java Teeing Collectors","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"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'll take a closer look at the Teeing collector and how you can use it in your\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\/lost-places-g4dcac2ba2_640.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\/2023\/06\/lost-places-g4dcac2ba2_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/lost-places-g4dcac2ba2_640.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3340,"url":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/java-collectors\/","url_meta":{"origin":2404,"position":1},"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":2473,"url":"https:\/\/www.mymiller.name\/wordpress\/pipelines\/java-pipelines\/","url_meta":{"origin":2404,"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":2404,"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":2233,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/javas-missing-treemap\/","url_meta":{"origin":2404,"position":4},"title":"Java&#8217;s missing TreeMap","author":"Jeffery Miller","date":"December 18, 2025","format":false,"excerpt":"One thing I have always found lacking in Java collections\/containers is TreeMap. A simple class that takes a hierarchical approach to mapping data to a node.\u00a0 Given a hierarchical string like \"\/java\/myapp\/javafx\/config\/phone\" I can set the various objects relative to this on that node. Java Preferences() class supports this hierarchical\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2526,"url":"https:\/\/www.mymiller.name\/wordpress\/pipelines\/pipeline-switches\/","url_meta":{"origin":2404,"position":5},"title":"Pipeline Switches","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"One of the things that make pipelines attract is the ability to create switches. Now my question to you is what if it isn't the data your processing that should determine if a switch pushes the data down an alternate pipeline? We have several methods that perform this task filter()\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\/railway-station-1270893_640.jpg?fit=640%2C384&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/railway-station-1270893_640.jpg?fit=640%2C384&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/railway-station-1270893_640.jpg?fit=640%2C384&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\/2404","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=2404"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"predecessor-version":[{"id":2408,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2404\/revisions\/2408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2405"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2404"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}