Skip to content

Commit aa4dec2

Browse files
authored
Merge pull request #3 from durgesh137/modules-specific-change
new: addigg printing numbers with method reference and lambdas furthe…
2 parents 6fbe0ff + de96062 commit aa4dec2

File tree

6 files changed

+578
-0
lines changed

6 files changed

+578
-0
lines changed

java8/LEARNING_STRUCTURE.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# Java 8 Stream API Learning Structure
2+
3+
## Project Status (Last Updated: December 1, 2025)
4+
5+
6+
### Quick Status
7+
-**Core Examples** - FilterAndSum (Traditional + Stream + Comparison) - WORKING
8+
-**Q001: Print Numbers** - Complete (Traditional + Stream + Comparison) - WORKING
9+
10+
### Action Required
11+
Create `SumOfSquaresStream.java` to complete Q002 and enable its comparison runner.
12+
13+
---
14+
15+
## Overview
16+
17+
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:
18+
19+
- **Why** streams were introduced
20+
- **When** to use streams vs traditional loops
21+
- **How** to think functionally and transition from imperative to declarative style
22+
23+
## Package Structure (Current State)
24+
25+
26+
## What's Been Created (Current Files)
27+
28+
### 1. Practice Problems (Current)
29+
30+
#### ✅ Q001: Print All Numbers in a List
31+
**Location:** `problems/collections/Q001_print_numbers/`
32+
33+
**Problem:** Print all numbers in a list
34+
35+
**Input:** `[1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10]`
36+
37+
**Files:**
38+
-`PrintNumbersTraditional.java` - Uses for-each loop
39+
-`PrintNumbersStream.java` - Uses stream().forEach()
40+
-`PrintNumbersComparison.java` - Full comparison with performance analysis
41+
42+
**Key Concepts:**
43+
- Basic stream operations
44+
- Method references (`System.out::println`)
45+
- Lambda expressions
46+
- forEach() terminal operation
47+
48+
---
49+
50+
### 3. Documentation Files
51+
52+
-`README.md` - Module overview and resources
53+
-`LEARNING_STRUCTURE.md` - This file
54+
-`PROBLEM_TEMPLATE.md` - Template for adding new problems
55+
56+
#### PROBLEM_TEMPLATE.md
57+
A complete guide for adding new problems, including:
58+
- File structure templates
59+
- Code templates for Traditional/Stream/Comparison classes
60+
- Best practices
61+
- Problem ideas list (Easy/Medium/Hard)
62+
- Running instructions
63+
64+
#### Updated README.md
65+
- Project structure diagram
66+
- Learning approach explanation
67+
- Running examples
68+
- Java 8 resources
69+
70+
## Running the Examples
71+
72+
### Compile the module
73+
```bash
74+
mvn -pl java8 clean compile
75+
```
76+
77+
### ✅ Working Examples (Can Run Now)
78+
79+
#### 1. Q001: Print Numbers Comparison
80+
```bash
81+
mvn -pl java8 exec:java -Dexec.mainClass="com.modernjava.guide.java8.Q001_print_numbers.PrintNumbersComparison"
82+
```
83+
84+
**Output shows:**
85+
- Traditional for-each loop approach
86+
- Stream API with method references
87+
- Alternative stream variations (lambda, custom formatting)
88+
- Performance comparison with JIT warmup
89+
- Detailed pros/cons summary
90+
91+
---
92+
93+
## Learning Path (Based on Current Files)
94+
95+
### For Beginners (Start Here!)
96+
1.**Q001: Print Numbers** - The simplest example (WORKING)
97+
- Read `PrintNumbersTraditional.java` - understand basic for-each loop
98+
- Read `PrintNumbersStream.java` - see your first stream operation
99+
- Run `PrintNumbersComparison.java` - see them side-by-side
100+
3. Compare traditional vs stream code styles
101+
102+
## Problem Ideas to Implement Next
103+
104+
### ✅ Completed Problems
105+
- [x] **Q001**: Print all numbers in a list (collections) - **FULLY WORKING**
106+
107+
108+
### 📝 Easy Problems (Recommended Next)
109+
- [ ] **Q003**: Print even numbers only
110+
- [ ] **Q004**: Find sum of all numbers
111+
- [ ] **Q005**: Find max/min element
112+
- [ ] **Q006**: Count even vs odd numbers
113+
- [ ] **Q007**: Remove duplicates from list
114+
- [ ] **Q008**: Count occurrences of specific element
115+
- [ ] **Q009**: Convert list to uppercase strings
116+
- [ ] **Q010**: Check if any/all elements match condition
117+
118+
### 🔨 Medium Problems
119+
- [ ] **Q011**: Find numbers divisible by 3 and 5
120+
- [ ] **Q012**: Group strings by length
121+
- [ ] **Q013**: Find top K frequent elements
122+
- [ ] **Q014**: Flatten nested lists
123+
- [ ] **Q015**: Partition list into even/odd
124+
- [ ] **Q016**: Calculate average of filtered numbers
125+
- [ ] **Q017**: Find first N Fibonacci numbers
126+
- [ ] **Q018**: Merge and sort multiple lists
127+
- [ ] **Q019**: Find second highest number
128+
- [ ] **Q020**: Group by multiple criteria
129+
130+
### 🚀 Hard Problems
131+
- [ ] **Q021**: Find longest increasing subsequence length
132+
- [ ] **Q022**: Custom grouping with complex criteria
133+
- [ ] **Q023**: Complex transformations with multiple maps
134+
- [ ] **Q024**: Parallel processing optimization
135+
- [ ] **Q025**: Custom collectors implementation
136+
- [ ] **Q026**: Sliding window operations
137+
- [ ] **Q027**: Real-time data processing simulation
138+
- [ ] **Q028**: Complex nested object transformations
139+
- [ ] **Q029**: Performance optimization challenges
140+
- [ ] **Q030**: Stream debugging and error handling
141+
142+
## Key Benefits of This Structure
143+
144+
1. **Comparative Learning** - See both approaches side-by-side
145+
2. **Practical Examples** - Real problems with real solutions
146+
3. **Performance Insights** - Understand when streams are faster/slower
147+
4. **Executable Code** - Run and experiment immediately
148+
5. **Template-Driven** - Easy to add more problems
149+
6. **Well-Documented** - Each class has detailed javadoc
150+
151+
## Next Steps
152+
153+
### To Continue Learning
154+
1. **Add more problems** using `PROBLEM_TEMPLATE.md` as a guide
155+
2. **Follow the numbering pattern** - Q00X_problem_name for easy tracking
156+
3. **Always create 3 files per problem:**
157+
- `XxxTraditional.java` - Pre-Java 8 approach
158+
- `XxxStream.java` - Java 8 Stream API approach
159+
- `XxxComparison.java` - Side-by-side comparison with performance
160+
4. **Solve problems from** LeetCode/HackerRank both ways
161+
5. **Experiment** with different Stream operations:
162+
- `flatMap()` for nested structures
163+
- `groupingBy()` for complex aggregations
164+
- `partitioningBy()` for splitting data
165+
- Custom collectors
166+
167+
## Resources
168+
169+
- Java 8 Stream API Tutorial: https://docs.oracle.com/javase/tutorial/collections/streams/
170+
- Stream API Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
171+
- Problem Template: `java8/PROBLEM_TEMPLATE.md`
172+
- Troubleshooting: `docs/TROUBLESHOOTING.md`
173+

java8/PROBLEM_TEMPLATE.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Problem Template Guide
2+
3+
Use this template when adding new problems to the java8 module.
4+
5+
## File Structure for Each Problem
6+
7+
For a problem named `XYZ`, create three files:
8+
9+
1. `XYZTraditional.java` - Traditional pre-Java 8 solution
10+
2. `XYZStream.java` - Java 8 Stream API solution
11+
3. `XYZComparison.java` - Runner that compares both approaches
12+
13+
## Problem Ideas to Implement
14+
15+
### Easy
16+
- [ ] Find max/min element
17+
- [ ] Remove duplicates from list
18+
- [ ] Count occurrences of element
19+
- [ ] Convert list to uppercase strings
20+
- [ ] Check if any/all elements match condition
21+
22+
### Medium
23+
- [ ] Group strings by length
24+
- [ ] Find top K frequent elements
25+
- [ ] Flatten nested lists
26+
- [ ] Partition list into even/odd
27+
- [ ] Calculate average of filtered numbers
28+
29+
### Hard
30+
- [ ] Find longest increasing subsequence length
31+
- [ ] Custom grouping with multiple criteria
32+
- [ ] Complex transformations with multiple maps
33+
- [ ] Parallel processing optimization
34+
- [ ] Custom collectors
35+
36+
## Best Practices
37+
38+
1. **Always handle null/empty inputs** gracefully
39+
2. **Add complexity analysis** (time and space)
40+
3. **Include multiple test cases** (normal, edge, large)
41+
4. **Document why Stream API is better** (or when it's not)
42+
5. **Show alternative Stream implementations** when instructive
43+
6. **Compare performance** with realistic data sizes
44+
7. **Add comments explaining each stream operation**
45+
8. **Keep code simple and focused** on the concept being taught
46+
47+
## Running Your Problem
48+
49+
```bash
50+
# Compile
51+
mvn -pl java8 clean compile
52+
53+
# Run comparison
54+
mvn -pl java8 exec:java -Dexec.mainClass="com.modernjava.guide.java8.Q001_print_numbers.PrintNumbersComparison"
55+
```
56+

java8/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,46 @@ mvn -Pbuild-java8 clean package
6161
./scripts/generate-toolchains-macos.sh --apply
6262
```
6363

64+
## Project Structure
65+
66+
This module is organized to help you learn Stream API by comparing it with traditional approaches:
67+
68+
69+
### Learning Approach
70+
71+
Each problem demonstrates:
72+
1. **Traditional Solution** - Pre-Java 8 imperative style with loops
73+
2. **Stream Solution** - Java 8+ functional style with Stream API
74+
3. **Comparison Runner** - Side-by-side execution with performance metrics
75+
76+
Example problems:
77+
- **Filter and Sum**: Basic filtering and aggregation operations
78+
- **Sum of Squares of Odd Numbers**: Combining filter, map, and reduce operations
79+
- More coming soon...
80+
81+
### Running Examples
82+
83+
```bash
84+
# Compile the module
85+
mvn -pl java8 clean compile
86+
```
87+
88+
## Java 8 Features & Resources
89+
90+
Features newly included within Java 8: https://www.oracle.com/java/technologies/javase/8-whats-new.html
91+
92+
Key topics to explore:
93+
- **Stream API**: https://docs.oracle.com/javase/tutorial/collections/streams/
94+
- **Lambda Expressions**: Functional interfaces and method references
95+
- **Optional**: Better null handling
96+
- **Date/Time API**: Modern date and time handling
97+
- **Default Methods**: Interface evolution
98+
99+
Additional resources:
100+
- Java learning path: https://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html
101+
- Java 8 documentation: https://docs.oracle.com/javase/8/index.html
102+
- GC tuning: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/
103+
64104
Notes:
65105
- 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.
66106
- See `docs/TOOLCHAINS.md` for more details.

0 commit comments

Comments
 (0)