|
1 | 1 | # Java 8 Problems & Concepts |
2 | 2 |
|
3 | | -## Completed Problems |
| 3 | +## 📂 Module Structure |
4 | 4 |
|
5 | | -### ✅ Q001: Print All Numbers |
6 | | -**Location:** `java8/Q001_print_numbers/` |
| 5 | +``` |
| 6 | +java8/ |
| 7 | +├── pom.xml # Module build configuration |
| 8 | +├── README.md # Module overview and quick start |
| 9 | +├── PROBLEMS.md # This file - problem statements and status |
| 10 | +├── LEARNING_STRUCTURE.md # Complete learning guide |
| 11 | +├── PROBLEM_TEMPLATE.md # Template for adding new problems |
| 12 | +├── Troubleshoot.md # Common errors and solutions |
| 13 | +├── src/ |
| 14 | +│ └── main/ |
| 15 | +│ └── java/ |
| 16 | +│ └── com/modernjava/guide/ |
| 17 | +│ ├── Main.java # Module entry point |
| 18 | +│ └── java8/ |
| 19 | +│ ├── Q001_print_numbers/ |
| 20 | +│ │ ├── PrintNumbersComparison.java # ✅ Complete |
| 21 | +│ │ ├── PrintNumbersStream.java # ✅ Complete |
| 22 | +│ │ └── PrintNumbersTraditional.java # ✅ Complete |
| 23 | +│ ├── Q002_print_evens/ |
| 24 | +│ │ ├── PrintEvensComparison.java # ✅ Complete |
| 25 | +│ │ ├── PrintEvensStream.java # ✅ Complete |
| 26 | +│ │ └── PrintEvensTraditional.java # ✅ Complete |
| 27 | +│ ├── Q003_square_nums/ |
| 28 | +│ │ ├── SumOfSquareOfNumberComparison.java # ✅ Complete |
| 29 | +│ │ ├── SumOfSquareOfNumberStream.java # ✅ Complete |
| 30 | +│ │ └── SumOfSquareOfNumberTraditional.java # ✅ Complete |
| 31 | +│ ├── Q004_max_min/ |
| 32 | +│ │ ├── MaxMinNumberComparison.java # ✅ Complete |
| 33 | +│ │ ├── MaxMinNumberStream.java # ✅ Complete |
| 34 | +│ │ ├── MaxMinNumberTraditional.java # ✅ Complete |
| 35 | +│ │ └── Pair.java # ✅ Helper class |
| 36 | +│ └── util/ |
| 37 | +│ └── ComparisonUtils.java # ✅ Shared utilities |
| 38 | +└── target/ # Compiled output (not tracked in git) |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## ✅ Completed Problems |
| 44 | + |
| 45 | +### Q001: Print All Numbers |
| 46 | +**Location:** `com.modernjava.guide.java8.Q001_print_numbers` |
7 | 47 | **Concepts:** Stream basics, forEach(), method references, lambda expressions |
8 | | -**Problem:** Print all numbers in a list |
| 48 | +**Problem:** Print all numbers in an array |
9 | 49 | **Input:** `[1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10]` |
10 | 50 | **Traditional:** for-each loop |
11 | | -**Stream:** `numbers.stream().forEach(System.out::println)` |
| 51 | +**Stream:** `Arrays.stream(numbers).forEach(System.out::println)` |
| 52 | +**Comparison:** ✅ `PrintNumbersComparison.java` |
12 | 53 |
|
13 | 54 | --- |
14 | 55 |
|
15 | | -## Partially Completed |
16 | | - |
17 | | -### ⚠️ Q002: Print Even numbers |
18 | | -**Location:** `java8/Q002_print_evens/` |
19 | | -**Concepts:** filter(), forEach(), method references, lambda expressions |
20 | | -**Problem:** Print all even numbers present within a list |
21 | | -**Input:** `[1, 2, 3, 4, 5]` → **Output:** `2,4` |
22 | | -**Traditional:** ✅ `PrintEvensTraditional.java` |
23 | | -**Stream:** ✅ `PrintEvensStream.java` |
| 56 | +### Q002: Print Even Numbers |
| 57 | +**Location:** `com.modernjava.guide.java8.Q002_print_evens` |
| 58 | +**Concepts:** filter(), forEach(), predicates, lambda expressions |
| 59 | +**Problem:** Print all even numbers present within an array |
| 60 | +**Input:** `[1, 2, 3, 4, 5]` → **Output:** `2, 4` |
| 61 | +**Traditional:** ✅ `PrintEvensTraditional.java` - for loop with modulo check |
| 62 | +**Stream:** ✅ `PrintEvensStream.java` - `filter(n -> n % 2 == 0)` |
24 | 63 | **Comparison:** ✅ `PrintEvensComparison.java` |
25 | 64 |
|
26 | 65 | --- |
27 | 66 |
|
28 | | -## Core Concepts Covered |
| 67 | +### Q003: Sum of Square of Numbers |
| 68 | +**Location:** `com.modernjava.guide.java8.Q003_square_nums` |
| 69 | +**Concepts:** map(), reduce(), sum(), transformations |
| 70 | +**Problem:** Calculate sum of squares of all numbers in an array |
| 71 | +**Input:** `[1, 2, 3, 4, 5]` → **Output:** `55` (1² + 2² + 3² + 4² + 5²) |
| 72 | +**Traditional:** ✅ `SumOfSquareOfNumberTraditional.java` - for loop with accumulator |
| 73 | +**Stream:** ✅ `SumOfSquareOfNumberStream.java` - `map(n -> n * n).sum()` |
| 74 | +**Comparison:** ✅ `SumOfSquareOfNumberComparison.java` - includes performance benchmarks |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +### Q004: Find Max and Min Numbers |
| 79 | +**Location:** `com.modernjava.guide.java8.Q004_max_min` |
| 80 | +**Concepts:** max(), min(), Comparator, Optional handling |
| 81 | +**Problem:** Find both maximum and minimum values in an array |
| 82 | +**Input:** `[45, 22, 89, 11, 67, 34, 90, 5, -121, 342, 0, 9999, -5000]` |
| 83 | +**Output:** `Max = 9999, Min = -5000` |
| 84 | +**Traditional:** ✅ `MaxMinNumberTraditional.java` - single pass with variables |
| 85 | +**Stream:** ✅ `MaxMinNumberStream.java` - `max()` and `min()` terminal operations |
| 86 | +**Comparison:** ✅ `MaxMinNumberComparison.java` - multiple test cases with varying sizes |
| 87 | +**Helper:** ✅ `Pair.java` - simple class to hold max/min result |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 🛠️ Utility Classes |
| 92 | + |
| 93 | +### ComparisonUtils |
| 94 | +**Location:** `com.modernjava.guide.java8.util.ComparisonUtils` |
| 95 | +**Purpose:** Shared helper methods for all comparison classes |
| 96 | + |
| 97 | +**Methods:** |
| 98 | +- `repeat(String str, int count)` - Repeats string N times (Java 8 compatible alternative to String.repeat()) |
| 99 | +- `formatTime(long nanos)` - Formats nanoseconds to human-readable format (ns, μs, ms) |
| 100 | +- `printNumbers(int[] numbers)` - Prints array elements comma-separated |
| 101 | +- `getArrayOfSpecifiedSize(int size)` - Generates random int array with positive, negative, and zero values (range: -100 to +100) |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## 🎯 Core Concepts Covered |
29 | 106 |
|
30 | 107 | ### Stream Operations |
31 | | -- **Terminal Operations:** `forEach()`, `count()`, `sum()` |
| 108 | +- **Terminal Operations:** `forEach()`, `count()`, `sum()`, `max()`, `min()` |
32 | 109 | - **Intermediate Operations:** `filter()`, `map()` |
33 | | -- **Method References:** `System.out::println`, `Integer::intValue` |
| 110 | +- **Method References:** `System.out::println`, `Integer::compare` |
34 | 111 | - **Lambda Expressions:** `n -> n % 2 == 0`, `n -> n * n` |
35 | 112 |
|
36 | 113 | ### Comparison Topics |
37 | 114 | - Traditional vs Stream syntax |
38 | 115 | - Performance analysis with JIT warmup |
39 | 116 | - Code readability and maintainability |
40 | 117 | - When to use each approach |
| 118 | +- Handling edge cases (empty arrays, single elements) |
| 119 | + |
| 120 | +### Java 8 Features Demonstrated |
| 121 | +- ✅ Lambda expressions |
| 122 | +- ✅ Method references |
| 123 | +- ✅ Stream API basics |
| 124 | +- ✅ Functional interfaces |
| 125 | +- ✅ Optional (in max/min operations) |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## 📊 Performance Notes |
| 130 | + |
| 131 | +Based on benchmark results in comparison classes: |
| 132 | +- **Small arrays (< 100 elements):** Traditional approach is faster due to Stream overhead |
| 133 | +- **Large arrays (10,000+ elements):** Stream performance improves, gap narrows |
| 134 | +- **Very large arrays (1,000,000+ elements):** Traditional still edges out for simple operations |
| 135 | +- **Takeaway:** Use Streams for readability and composition, not raw speed in tight loops |
41 | 136 |
|
42 | 137 | --- |
43 | 138 |
|
44 | | -## Planned Problems |
| 139 | +## 🚧 Planned Problems |
45 | 140 |
|
46 | 141 | ### Easy |
47 | 142 | - Q003: Print even numbers only |
|
0 commit comments