This repository contains practical examples of Java's new Stream Gatherers API, demonstrating real-world use cases with detailed implementations.
Stream Gatherers is a new feature introduced in Java that provides powerful stream processing capabilities. This project showcases five different gatherers through practical business scenarios.
- Java 24 (Early Access)
- Maven 3.6+
Package: org.phoenix.gatherer._01_gatherer_fold
Demonstrates accumulating operations by calculating shopping cart totals with discounts and taxes.
// Example: Calculate total price with running totals
stream.gather(Gatherers.fold(initial, (state, item) -> state.add(item)))Package: org.phoenix.gatherer._02_gatherer_scan
Shows intermediate accumulation states by tracking credit card transactions and balance changes.
// Example: Track balance after each transaction
stream.gather(Gatherers.scan(initial, (state, transaction) -> state.apply(transaction)))Package: org.phoenix.gatherer._03_gatherer_windowfixed
Processes data in fixed-size batches, perfect for API rate limiting scenarios.
// Example: Process customers in batches of 100
stream.gather(Gatherers.windowFixed(100))Package: org.phoenix.gatherer._04_gatherer_windowsliding
Calculates moving averages and generates buy/sell signals using sliding windows.
// Example: 5-day moving average
stream.gather(Gatherers.windowSliding(5))Package: org.phoenix.gatherer._05_gatherers_mapconcurrent
Fetches data from multiple sources in parallel (flights, hotels, weather) with controlled concurrency.
// Example: Process 3 requests concurrently
stream.gather(Gatherers.mapConcurrent(3, asyncOperation))- Clone the repository:
git clone https://github.com/yourusername/stream-gatherers.git
cd stream-gatherers- Build the project:
mvn clean compile- Run individual examples:
# Shopping Cart Summary
mvn exec:java -Dexec.mainClass="org.phoenix.gatherer._01_gatherer_fold.ShoppingCartApp"
# Credit Card Tracking
mvn exec:java -Dexec.mainClass="org.phoenix.gatherer._02_gatherer_scan.CreditCardTracking"
# Bulk SMS System
mvn exec:java -Dexec.mainClass="org.phoenix.gatherer._03_gatherer_windowfixed.BulkSMSSystem"
# Stock Technical Analysis
mvn exec:java -Dexec.mainClass="org.phoenix.gatherer._04_gatherer_windowsliding.StockTechnicalAnalysis"
# Travel Data Aggregator
mvn exec:java -Dexec.mainClass="org.phoenix.gatherer._05_gatherers_mapconcurrent.TravelDataAggregator"- fold: Efficient accumulation without intermediate collections
- scan: Stream processing with state visibility
- windowFixed: Batch processing for API limits
- windowSliding: Rolling calculations without manual buffering
- mapConcurrent: Parallel processing with backpressure control
- Type Safety: Full type inference and compile-time checking
- Composability: Gatherers can be chained with other stream operations
- Performance: Optimized for large data sets
- Flexibility: Custom gatherers can be implemented
- Java 24 Stream Gatherers - The New Kid in Streams
- JEP 473: Stream Gatherers
- Java 24 Early Access Downloads
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
This project is licensed under the MIT License - see the LICENSE file for details.
- Phoenix Team
- Java Community for continuous innovation
- OpenJDK team for Stream Gatherers implementation
- Medium article by @javatechie for inspiration