Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions java8/LEARNING_STRUCTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Java 8 Stream API Learning Structure

## Project Status (Last Updated: December 1, 2025)


### Quick Status
- ✅ **Core Examples** - FilterAndSum (Traditional + Stream + Comparison) - WORKING
- ✅ **Q001: Print Numbers** - Complete (Traditional + Stream + Comparison) - WORKING

### Action Required
Create `SumOfSquaresStream.java` to complete Q002 and enable its comparison runner.

---

## Overview

The java8 module has been set up with a comprehensive structure to learn Stream API by comparing it with traditional pre-Java 8 approaches. This allows you to understand:

- **Why** streams were introduced
- **When** to use streams vs traditional loops
- **How** to think functionally and transition from imperative to declarative style

## Package Structure (Current State)


## What's Been Created (Current Files)

### 1. Practice Problems (Current)

#### ✅ Q001: Print All Numbers in a List
**Location:** `problems/collections/Q001_print_numbers/`

**Problem:** Print all numbers in a list

**Input:** `[1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10]`

**Files:**
- ✅ `PrintNumbersTraditional.java` - Uses for-each loop
- ✅ `PrintNumbersStream.java` - Uses stream().forEach()
- ✅ `PrintNumbersComparison.java` - Full comparison with performance analysis

**Key Concepts:**
- Basic stream operations
- Method references (`System.out::println`)
- Lambda expressions
- forEach() terminal operation

---

### 3. Documentation Files

- ✅ `README.md` - Module overview and resources
- ✅ `LEARNING_STRUCTURE.md` - This file
- ✅ `PROBLEM_TEMPLATE.md` - Template for adding new problems

#### PROBLEM_TEMPLATE.md
A complete guide for adding new problems, including:
- File structure templates
- Code templates for Traditional/Stream/Comparison classes
- Best practices
- Problem ideas list (Easy/Medium/Hard)
- Running instructions

#### Updated README.md
- Project structure diagram
- Learning approach explanation
- Running examples
- Java 8 resources

## Running the Examples

### Compile the module
```bash
mvn -pl java8 clean compile
```

### ✅ Working Examples (Can Run Now)

#### 1. Q001: Print Numbers Comparison
```bash
mvn -pl java8 exec:java -Dexec.mainClass="com.modernjava.guide.java8.Q001_print_numbers.PrintNumbersComparison"
```

**Output shows:**
- Traditional for-each loop approach
- Stream API with method references
- Alternative stream variations (lambda, custom formatting)
- Performance comparison with JIT warmup
- Detailed pros/cons summary

---

## Learning Path (Based on Current Files)

### For Beginners (Start Here!)
1. ✅ **Q001: Print Numbers** - The simplest example (WORKING)
- Read `PrintNumbersTraditional.java` - understand basic for-each loop
- Read `PrintNumbersStream.java` - see your first stream operation
- Run `PrintNumbersComparison.java` - see them side-by-side
3. Compare traditional vs stream code styles

## Problem Ideas to Implement Next

### ✅ Completed Problems
- [x] **Q001**: Print all numbers in a list (collections) - **FULLY WORKING**


### 📝 Easy Problems (Recommended Next)
- [ ] **Q003**: Print even numbers only
- [ ] **Q004**: Find sum of all numbers
- [ ] **Q005**: Find max/min element
- [ ] **Q006**: Count even vs odd numbers
- [ ] **Q007**: Remove duplicates from list
- [ ] **Q008**: Count occurrences of specific element
- [ ] **Q009**: Convert list to uppercase strings
- [ ] **Q010**: Check if any/all elements match condition

### 🔨 Medium Problems
- [ ] **Q011**: Find numbers divisible by 3 and 5
- [ ] **Q012**: Group strings by length
- [ ] **Q013**: Find top K frequent elements
- [ ] **Q014**: Flatten nested lists
- [ ] **Q015**: Partition list into even/odd
- [ ] **Q016**: Calculate average of filtered numbers
- [ ] **Q017**: Find first N Fibonacci numbers
- [ ] **Q018**: Merge and sort multiple lists
- [ ] **Q019**: Find second highest number
- [ ] **Q020**: Group by multiple criteria

### 🚀 Hard Problems
- [ ] **Q021**: Find longest increasing subsequence length
- [ ] **Q022**: Custom grouping with complex criteria
- [ ] **Q023**: Complex transformations with multiple maps
- [ ] **Q024**: Parallel processing optimization
- [ ] **Q025**: Custom collectors implementation
- [ ] **Q026**: Sliding window operations
- [ ] **Q027**: Real-time data processing simulation
- [ ] **Q028**: Complex nested object transformations
- [ ] **Q029**: Performance optimization challenges
- [ ] **Q030**: Stream debugging and error handling

## Key Benefits of This Structure

1. **Comparative Learning** - See both approaches side-by-side
2. **Practical Examples** - Real problems with real solutions
3. **Performance Insights** - Understand when streams are faster/slower
4. **Executable Code** - Run and experiment immediately
5. **Template-Driven** - Easy to add more problems
6. **Well-Documented** - Each class has detailed javadoc

## Next Steps

### To Continue Learning
1. **Add more problems** using `PROBLEM_TEMPLATE.md` as a guide
2. **Follow the numbering pattern** - Q00X_problem_name for easy tracking
3. **Always create 3 files per problem:**
- `XxxTraditional.java` - Pre-Java 8 approach
- `XxxStream.java` - Java 8 Stream API approach
- `XxxComparison.java` - Side-by-side comparison with performance
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent hyphenation in "Side-by-side" vs "side-by-side". For consistency within the same document, use lowercase "side-by-side" as it appears in line 145.

Current: Side-by-side comparison
Suggested: side-by-side comparison

Suggested change
- `XxxComparison.java` - Side-by-side comparison with performance
- `XxxComparison.java` - side-by-side comparison with performance

Copilot uses AI. Check for mistakes.
4. **Solve problems from** LeetCode/HackerRank both ways
5. **Experiment** with different Stream operations:
- `flatMap()` for nested structures
- `groupingBy()` for complex aggregations
- `partitioningBy()` for splitting data
- Custom collectors

## Resources

- Java 8 Stream API Tutorial: https://docs.oracle.com/javase/tutorial/collections/streams/
- Stream API Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- Problem Template: `java8/PROBLEM_TEMPLATE.md`
- Troubleshooting: `docs/TROUBLESHOOTING.md`

56 changes: 56 additions & 0 deletions java8/PROBLEM_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Problem Template Guide

Use this template when adding new problems to the java8 module.

## File Structure for Each Problem

For a problem named `XYZ`, create three files:

1. `XYZTraditional.java` - Traditional pre-Java 8 solution
2. `XYZStream.java` - Java 8 Stream API solution
3. `XYZComparison.java` - Runner that compares both approaches

## Problem Ideas to Implement

### Easy
- [ ] Find max/min element
- [ ] Remove duplicates from list
- [ ] Count occurrences of element
- [ ] Convert list to uppercase strings
- [ ] Check if any/all elements match condition

### Medium
- [ ] Group strings by length
- [ ] Find top K frequent elements
- [ ] Flatten nested lists
- [ ] Partition list into even/odd
- [ ] Calculate average of filtered numbers

### Hard
- [ ] Find longest increasing subsequence length
- [ ] Custom grouping with multiple criteria
- [ ] Complex transformations with multiple maps
- [ ] Parallel processing optimization
- [ ] Custom collectors

## Best Practices

1. **Always handle null/empty inputs** gracefully
2. **Add complexity analysis** (time and space)
3. **Include multiple test cases** (normal, edge, large)
4. **Document why Stream API is better** (or when it's not)
5. **Show alternative Stream implementations** when instructive
6. **Compare performance** with realistic data sizes
7. **Add comments explaining each stream operation**
8. **Keep code simple and focused** on the concept being taught

## Running Your Problem

```bash
# Compile
mvn -pl java8 clean compile

# Run comparison
mvn -pl java8 exec:java -Dexec.mainClass="com.modernjava.guide.java8.Q001_print_numbers.PrintNumbersComparison"
```

40 changes: 40 additions & 0 deletions java8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ mvn -Pbuild-java8 clean package
./scripts/generate-toolchains-macos.sh --apply
```

## Project Structure

This module is organized to help you learn Stream API by comparing it with traditional approaches:


### Learning Approach

Each problem demonstrates:
1. **Traditional Solution** - Pre-Java 8 imperative style with loops
2. **Stream Solution** - Java 8+ functional style with Stream API
3. **Comparison Runner** - Side-by-side execution with performance metrics

Example problems:
- **Filter and Sum**: Basic filtering and aggregation operations
- **Sum of Squares of Odd Numbers**: Combining filter, map, and reduce operations
- More coming soon...

### Running Examples

```bash
# Compile the module
mvn -pl java8 clean compile
```

## Java 8 Features & Resources

Features newly included within Java 8: https://www.oracle.com/java/technologies/javase/8-whats-new.html

Key topics to explore:
- **Stream API**: https://docs.oracle.com/javase/tutorial/collections/streams/
- **Lambda Expressions**: Functional interfaces and method references
- **Optional**: Better null handling
- **Date/Time API**: Modern date and time handling
- **Default Methods**: Interface evolution

Additional resources:
- Java learning path: https://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html
- Java 8 documentation: https://docs.oracle.com/javase/8/index.html
- GC tuning: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/

Notes:
- If you use the `release` compiler flag (the default in this repo), make sure Maven runs with a JDK version that supports that `--release` value or configure toolchains to supply the correct JDK to the compiler plugin.
- See `docs/TOOLCHAINS.md` for more details.
Loading