Code Explainers

Code explainers tagged #streams

java
package com.example.validation;
 
import java.util.List;
import java.util.stream.Collectors;

Validating JSON payloads against a schema in Java

json-schema validation recursion
Intermediate 8 steps
java
public class DuplicateFinder {
 
    public Map<String, List<Path>> findDuplicates(Path root) throws IOException {
        Map<String, List<Path>> byHash = new HashMap<>();

Finding duplicate files by content hash

hashing file-io streams
Intermediate 8 steps
javascript
import { NextResponse } from 'next/server';
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { pipeline } from 'node:stream/promises';

Streaming file uploads in a Next.js route

file-upload streams validation
Intermediate 9 steps
java
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

Ranking the most frequent words in Java

streams regex grouping
Intermediate 7 steps
rust
use axum::{
    extract::State,
    response::sse::{Event, KeepAlive, Sse},
};

Broadcasting live prices over SSE in Axum

server-sent-events broadcast-channel streams
Advanced 9 steps
java
public record FixedWidthField(String name, int start, int end) {
 
    public String extract(String line) {
        int from = Math.min(start, line.length());

Parsing fixed-width text in Java

records parsing streams
Intermediate 7 steps
java
@Controller
public class BookController {
 
    private final BookService bookService;

Solving GraphQL N+1 with batch mapping in Spring

graphql n+1-problem batching
Intermediate 8 steps
java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

Compressing byte arrays with GZIP in Java

compression streams try-with-resources
Intermediate 6 steps
java
public final class ZipExtractor {
 
    private final Path targetDir;
 

Extracting a zip safely in Java

path-traversal streams file-io
Intermediate 8 steps
typescript
type JsonValue = Record<string, unknown> | unknown[];
 
export function parseJsonStream<T = JsonValue>(
  stream: ReadableStream<Uint8Array>,

Streaming JSON parsing with a depth counter

streams parsing state-machine
Advanced 9 steps
java
public final class ListPartitioner {
 
    private ListPartitioner() {
    }

Splitting a list into fixed-size chunks in Java

generics partitioning streams
Intermediate 8 steps
java
import java.util.List;
import java.util.IntSummaryStatistics;
 
public class OrderReport {

Aggregating stats with IntSummaryStatistics

streams aggregation records
Intermediate 5 steps
java
public List<String> collectTagsFromArticles(List<Article> articles) {
    return articles.stream()
        .flatMap(article -> article.getTags().stream())
        .map(String::trim)

Flattening nested data with Java streams

streams flatmap collectors
Intermediate 7 steps
java
public final class FileChecksum {
 
    private static final char[] HEX = "0123456789abcdef".toCharArray();
 

Streaming a SHA-256 file checksum in Java

hashing streams resource-management
Intermediate 7 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) {
    return orders.stream()
            .collect(Collectors.groupingBy(Order::getCustomerId));
}

Grouping streams with Java Collectors

streams grouping collectors
Intermediate 5 steps
java
package com.example.logs;
 
import java.io.IOException;
import java.io.UncheckedIOException;

Counting HTTP statuses with Java streams

streams regex file-io
Intermediate 6 steps
java
public final class BusinessDays {
 
    private BusinessDays() {
    }

Counting business days between two dates in Java

date arithmetic utility class streams
Intermediate 7 steps
java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

Three ways to deduplicate a list by key in Java

streams deduplication collectors
Intermediate 6 steps