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
131 changes: 113 additions & 18 deletions java8/PROBLEMS.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,142 @@
# Java 8 Problems & Concepts

## Completed Problems
## 📂 Module Structure

### ✅ Q001: Print All Numbers
**Location:** `java8/Q001_print_numbers/`
```
java8/
├── pom.xml # Module build configuration
├── README.md # Module overview and quick start
├── PROBLEMS.md # This file - problem statements and status
├── LEARNING_STRUCTURE.md # Complete learning guide
├── PROBLEM_TEMPLATE.md # Template for adding new problems
├── Troubleshoot.md # Common errors and solutions
├── src/
│ └── main/
│ └── java/
│ └── com/modernjava/guide/
│ ├── Main.java # Module entry point
│ └── java8/
│ ├── Q001_print_numbers/
│ │ ├── PrintNumbersComparison.java # ✅ Complete
│ │ ├── PrintNumbersStream.java # ✅ Complete
│ │ └── PrintNumbersTraditional.java # ✅ Complete
│ ├── Q002_print_evens/
│ │ ├── PrintEvensComparison.java # ✅ Complete
│ │ ├── PrintEvensStream.java # ✅ Complete
│ │ └── PrintEvensTraditional.java # ✅ Complete
│ ├── Q003_square_nums/
│ │ ├── SumOfSquareOfNumberComparison.java # ✅ Complete
│ │ ├── SumOfSquareOfNumberStream.java # ✅ Complete
│ │ └── SumOfSquareOfNumberTraditional.java # ✅ Complete
│ ├── Q004_max_min/
│ │ ├── MaxMinNumberComparison.java # ✅ Complete
│ │ ├── MaxMinNumberStream.java # ✅ Complete
│ │ ├── MaxMinNumberTraditional.java # ✅ Complete
│ │ └── Pair.java # ✅ Helper class
│ └── util/
│ └── ComparisonUtils.java # ✅ Shared utilities
└── target/ # Compiled output (not tracked in git)
```

---

## ✅ Completed Problems

### Q001: Print All Numbers
**Location:** `com.modernjava.guide.java8.Q001_print_numbers`
**Concepts:** Stream basics, forEach(), method references, lambda expressions
**Problem:** Print all numbers in a list
**Problem:** Print all numbers in an array
**Input:** `[1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10]`
**Traditional:** for-each loop
**Stream:** `numbers.stream().forEach(System.out::println)`
**Stream:** `Arrays.stream(numbers).forEach(System.out::println)`
**Comparison:**`PrintNumbersComparison.java`

---

## Partially Completed

### ⚠️ Q002: Print Even numbers
**Location:** `java8/Q002_print_evens/`
**Concepts:** filter(), forEach(), method references, lambda expressions
**Problem:** Print all even numbers present within a list
**Input:** `[1, 2, 3, 4, 5]`**Output:** `2,4`
**Traditional:**`PrintEvensTraditional.java`
**Stream:**`PrintEvensStream.java`
### Q002: Print Even Numbers
**Location:** `com.modernjava.guide.java8.Q002_print_evens`
**Concepts:** filter(), forEach(), predicates, lambda expressions
**Problem:** Print all even numbers present within an array
**Input:** `[1, 2, 3, 4, 5]`**Output:** `2, 4`
**Traditional:**`PrintEvensTraditional.java` - for loop with modulo check
**Stream:**`PrintEvensStream.java` - `filter(n -> n % 2 == 0)`
**Comparison:**`PrintEvensComparison.java`

---

## Core Concepts Covered
### Q003: Sum of Square of Numbers
**Location:** `com.modernjava.guide.java8.Q003_square_nums`
**Concepts:** map(), reduce(), sum(), transformations
**Problem:** Calculate sum of squares of all numbers in an array
**Input:** `[1, 2, 3, 4, 5]`**Output:** `55` (1² + 2² + 3² + 4² + 5²)
**Traditional:**`SumOfSquareOfNumberTraditional.java` - for loop with accumulator
**Stream:**`SumOfSquareOfNumberStream.java` - `map(n -> n * n).sum()`
**Comparison:**`SumOfSquareOfNumberComparison.java` - includes performance benchmarks

---

### Q004: Find Max and Min Numbers
**Location:** `com.modernjava.guide.java8.Q004_max_min`
**Concepts:** max(), min(), Comparator, Optional handling
**Problem:** Find both maximum and minimum values in an array
**Input:** `[45, 22, 89, 11, 67, 34, 90, 5, -121, 342, 0, 9999, -5000]`
**Output:** `Max = 9999, Min = -5000`
**Traditional:**`MaxMinNumberTraditional.java` - single pass with variables
**Stream:**`MaxMinNumberStream.java` - `max()` and `min()` terminal operations
**Comparison:**`MaxMinNumberComparison.java` - multiple test cases with varying sizes
**Helper:**`Pair.java` - simple class to hold max/min result

---

## 🛠️ Utility Classes

### ComparisonUtils
**Location:** `com.modernjava.guide.java8.util.ComparisonUtils`
**Purpose:** Shared helper methods for all comparison classes

**Methods:**
- `repeat(String str, int count)` - Repeats string N times (Java 8 compatible alternative to String.repeat())
- `formatTime(long nanos)` - Formats nanoseconds to human-readable format (ns, μs, ms)
- `printNumbers(int[] numbers)` - Prints array elements comma-separated
- `getArrayOfSpecifiedSize(int size)` - Generates random int array with positive, negative, and zero values (range: -100 to +100)

---

## 🎯 Core Concepts Covered

### Stream Operations
- **Terminal Operations:** `forEach()`, `count()`, `sum()`
- **Terminal Operations:** `forEach()`, `count()`, `sum()`, `max()`, `min()`
- **Intermediate Operations:** `filter()`, `map()`
- **Method References:** `System.out::println`, `Integer::intValue`
- **Method References:** `System.out::println`, `Integer::compare`
- **Lambda Expressions:** `n -> n % 2 == 0`, `n -> n * n`

### Comparison Topics
- Traditional vs Stream syntax
- Performance analysis with JIT warmup
- Code readability and maintainability
- When to use each approach
- Handling edge cases (empty arrays, single elements)

### Java 8 Features Demonstrated
- ✅ Lambda expressions
- ✅ Method references
- ✅ Stream API basics
- ✅ Functional interfaces
- ✅ Optional (in max/min operations)

---

## 📊 Performance Notes

Based on benchmark results in comparison classes:
- **Small arrays (< 100 elements):** Traditional approach is faster due to Stream overhead
- **Large arrays (10,000+ elements):** Stream performance improves, gap narrows
- **Very large arrays (1,000,000+ elements):** Traditional still edges out for simple operations
- **Takeaway:** Use Streams for readability and composition, not raw speed in tight loops

---

## Planned Problems
## 🚧 Planned Problems

### Easy
- Q003: Print even numbers only
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

The "Planned Problems" section still lists "Q003: Print even numbers only" which is incorrect as Q003 is actually about "Sum of Square of Numbers" (already completed). This entry should be removed or updated to reflect remaining work.

Suggested change
- Q003: Print even numbers only

Copilot uses AI. Check for mistakes.
Expand Down
Loading