Skip to content

contains practical examples of Java new Stream Gatherers API, demonstrating real-world use cases with detailed implementations.

Notifications You must be signed in to change notification settings

asafacihangir/java-stream-gatherer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Java Stream Gatherers Examples

This repository contains practical examples of Java's new Stream Gatherers API, demonstrating real-world use cases with detailed implementations.

🚀 Overview

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.

📋 Prerequisites

  • Java 24 (Early Access)
  • Maven 3.6+

🛠️ Built-in Gatherers

1. Gatherers.fold - Shopping Cart Summary

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)))

2. Gatherers.scan - Credit Card Statement

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)))

3. Gatherers.windowFixed - Bulk SMS System

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))

4. Gatherers.windowSliding - Stock Technical Analysis

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))

5. Gatherers.mapConcurrent - Travel Data Aggregator

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))

🏃 Running the Examples

  1. Clone the repository:
git clone https://github.com/yourusername/stream-gatherers.git
cd stream-gatherers
  1. Build the project:
mvn clean compile
  1. 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"

📊 Performance Benefits

  • 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

🔍 Key Features

  • 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

📚 Resources

🤝 Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

👥 Author

  • Phoenix Team

🙏 Acknowledgments

  • Java Community for continuous innovation
  • OpenJDK team for Stream Gatherers implementation
  • Medium article by @javatechie for inspiration

About

contains practical examples of Java new Stream Gatherers API, demonstrating real-world use cases with detailed implementations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages