<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zach Maddox on Medium]]></title>
        <description><![CDATA[Stories by Zach Maddox on Medium]]></description>
        <link>https://medium.com/@zmad5306?source=rss-2d744795072d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QqinKj8xnq7WRsJy032jSg.jpeg</url>
            <title>Stories by Zach Maddox on Medium</title>
            <link>https://medium.com/@zmad5306?source=rss-2d744795072d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 04:51:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zmad5306/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Chunking Java Streams the Right Way — Finally, a Collector That Feels Like It Should Be in the JDK]]></title>
            <link>https://medium.com/@zmad5306/chunking-java-streams-the-right-way-finally-a-collector-that-feels-like-it-should-be-in-the-jdk-3d1e60d15c58?source=rss-2d744795072d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d1e60d15c58</guid>
            <dc:creator><![CDATA[Zach Maddox]]></dc:creator>
            <pubDate>Thu, 11 Dec 2025 15:56:00 GMT</pubDate>
            <atom:updated>2025-12-12T20:57:34.287Z</atom:updated>
            <content:encoded><![CDATA[<h3>Chunking Java Streams the Right Way — Finally, a Collector That Feels Like It Should Be in the JDK</h3><p>If you’ve ever needed to split a large list or stream into evenly sized chunks, you already know the pain:</p><ul><li>You write a loop.</li><li>Or worse, nested loops.</li><li>Maybe a counter.</li><li>Maybe a temporary list.</li><li>Maybe something that <em>almost</em> works until one edge case blows it up.</li></ul><p>Chunking elements is one of those <strong>everyday operations</strong> that <em>somehow never made it into the JDK</em>. So developers keep rewriting the same utility method in every project… slightly different each time.</p><p>After doing this one too many times — and hitting a PostgreSQL driver limitation that forced me to batch thousands of UUIDs into smaller chunks—I finally decided:</p><blockquote><strong><em>This should be a Collector.</em></strong><em><br> Clean. Composable. Built for Streams.</em></blockquote><p>So I built one.</p><p>This is <strong>Chunking Collector</strong> — a lightweight Java 8+ library that lets you express chunking in a way that <em>reads like it belongs in the standard library</em>.</p><h3>🔥 The Old Way: Manual Chunking (A Bit of a Mess)</h3><p>Here’s what most of us end up writing:</p><pre>List&lt;List&lt;T&gt;&gt; chunks = new ArrayList&lt;&gt;();<br>List&lt;T&gt; current = new ArrayList&lt;&gt;();</pre><pre>for (T item : items) {<br>    current.add(item);<br>    if (current.size() == chunkSize) {<br>        chunks.add(current);<br>        current = new ArrayList&lt;&gt;();<br>    }<br>}</pre><pre>if (!current.isEmpty()) {<br>    chunks.add(current);<br>}</pre><p>It works… until it doesn’t:</p><ul><li>Harder to read</li><li>Easy to get wrong</li><li>Not reusable</li><li>Not parallel-friendly</li><li>Not stream-friendly</li></ul><p>It also <strong>breaks the flow</strong> of code that naturally wants to be expressed as a Stream pipeline.</p><h3>✨ The New Way: A Collector That Just Works</h3><p>With <strong>Chunking Collector</strong>, you simply write:</p><pre>List&lt;List&lt;Integer&gt;&gt; chunks = numbers.stream()<br>    .collect(Chunking.toChunks(3));</pre><p>Output:</p><pre>[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]</pre><p>That’s it.<br> Readable. Safe. Predictable.</p><p>This is how chunking <em>should</em> feel.</p><h3>🧩 Why a Collector?</h3><p>Because chunking is fundamentally a <strong>reduction operation</strong>:</p><ul><li>A Stream goes in</li><li>A List of Lists comes out</li><li>No side effects</li><li>No mutation leaking out</li><li>Works naturally with <strong>ordered</strong>, <strong>parallel</strong>, or <strong>sequential</strong> streams</li></ul><p>And importantly, this fits the Stream philosophy perfectly:</p><pre>stream.collect(Chunking.toChunks(size));</pre><p>You immediately know what it does.</p><h3>📦 Installation</h3><pre>&lt;dependency&gt;<br>  &lt;groupId&gt;dev.zachmaddox&lt;/groupId&gt;<br>  &lt;artifactId&gt;chunking-collector&lt;/artifactId&gt;<br>  &lt;version&gt;1.2.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p>or:</p><pre>implementation &#39;dev.zachmaddox:chunking-collector:1.2.0&#39;</pre><h3>🧠 Practical Examples That Come Up All the Time</h3><h3>1. Batch Processing</h3><pre>Chunking.chunk(records, 100)<br>    .forEach(batch -&gt; processBatch(batch));</pre><h3>2. Database Paging</h3><pre>var pages = results.stream()<br>    .collect(Chunking.toChunks(500));</pre><h3>3. Parallel Workloads</h3><pre>Chunking.chunk(items, 10)<br>    .parallelStream()<br>    .forEach(this::processChunk);</pre><h3>🔥 The Real Origin: Working Around PostgreSQL IN-Clause Limits</h3><p>PostgreSQL (and many JDBC drivers) limit how large an argument list can be in a single SQL statement.</p><p>Chunking solves this cleanly and safely using parameterized SQL:</p><pre>NamedParameterJdbcTemplate named = new NamedParameterJdbcTemplate(jdbcTemplate);</pre><pre>Chunking.chunk(ids, 500)<br>    .parallelStream()<br>    .map(chunk -&gt; named.query(<br>        &quot;SELECT * FROM users WHERE id IN (:ids)&quot;,<br>        Map.of(&quot;ids&quot;, chunk),<br>        (rs, n) -&gt; mapRow(rs)<br>    ))<br>    .flatMap(List::stream)<br>    .toList();</pre><p>The result:</p><ul><li>No driver errors</li><li>Smaller, faster queries</li><li>Clear, maintainable code</li><li>Parallelizable workloads</li></ul><p>This alone justified building the library.</p><h3>⚡ Advanced Capabilities (When You Need Them)</h3><p>Chunking Collector has grown into a flexible toolkit:</p><ul><li><strong>Remainder policies</strong> (INCLUDE_PARTIAL, DROP_PARTIAL)</li><li><strong>Custom list factories</strong></li><li><strong>Lazy chunk streaming</strong></li><li><strong>Sliding windows</strong></li><li><strong>Boundary-based chunking</strong></li><li><strong>Weighted chunking</strong></li><li><strong>Primitive stream helpers</strong></li></ul><p>But the core API remains dead simple.</p><h3>🧩 Design Philosophy</h3><blockquote><em>“If this API ever became part of the JDK, nobody should be surprised.”</em></blockquote><p>So:</p><ul><li>No dependencies</li><li>No reflection</li><li>No magic</li><li>Just clean Java</li><li>Very small surface area</li><li>Behaves exactly how experienced Java devs expect</li></ul><h3>📚 Full Documentation</h3><p>JavaDoc: <a href="https://zmad5306.github.io/chunking-collector/latest/">https://zachmaddox.dev/ChunkingCollector/latest/</a></p><p>GitHub: <a href="https://github.com/zmad5306/chunking-collector">https://github.com/zmad5306/ChunkingCollector</a></p><p>Maven Central: <a href="https://central.sonatype.com/artifact/dev.zachmaddox/chunking-collector">https://central.sonatype.com/artifact/dev.zachmaddox/chunking-collector</a></p><h3>🎉 Final Thoughts</h3><p>Chunking is a <strong>universal problem</strong>, and now there’s finally a clean, reusable, stream-friendly solution for it.</p><p>If you’ve ever thought:</p><blockquote><em>“Why isn’t there just a built-in way to do this?”</em></blockquote><p>Well… now there is.</p><p>Give it a try, star the repo, and drop feedback — I’d love to hear how you’re using it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d1e60d15c58" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>