-
Notifications
You must be signed in to change notification settings - Fork 0
new: addigg printing numbers with method reference and lambdas furthe… #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a comprehensive Java 8 Stream API learning module focused on comparing traditional (pre-Java 8) and functional programming approaches. It adds the first practice problem (Q001: Print Numbers) along with supporting documentation and templates for future problems. The module aims to help developers understand when and how to use Stream APIs by providing side-by-side comparisons with traditional for-loop implementations, including performance analysis.
Key Changes
- Added Q001 Print Numbers example with three implementations: traditional for-loop, Stream API with method references, and Stream API with lambdas
- Created comprehensive comparison runner with performance benchmarking and detailed analysis of pros/cons for each approach
- Introduced structured documentation including README updates, problem template guide, and learning path structure
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| PrintNumbersTraditional.java | Implements traditional for-each loop approach for printing numbers from a list |
| PrintNumbersStream.java | Demonstrates Stream API approaches using both method references and lambda expressions |
| PrintNumbersComparison.java | Provides comprehensive comparison with performance testing, JIT warmup, and detailed pros/cons analysis |
| README.md | Updates module documentation with project structure, learning approach, and Java 8 resources |
| PROBLEM_TEMPLATE.md | Introduces standardized template for adding new practice problems with best practices |
| LEARNING_STRUCTURE.md | Documents the complete learning structure, current status, and roadmap for future problems |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| public class PrintNumbersStream { | ||
| public static void main(String[] args) { | ||
| System.out.println("Print all numbers in the list - Functional way"); | ||
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in the numbers list initialization. For consistency and readability, add a space after the comma between '10' and '1'.
Current: ..., 9, 10,1, 2, ...
Suggested: ..., 9, 10, 1, 2, ...
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); | |
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10, 1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in the numbers list initialization. For consistency and readability, add a space after the comma between '10' and '1'.
Current: ..., 9, 10,1, 2, ...
Suggested: ..., 9, 10, 1, 2, ...
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); | |
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10, 1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
| private static void printTraditionalSilent(List<Integer> numbers) { | ||
| for (Integer number : numbers) { | ||
| // No output for performance testing | ||
| number.toString(); |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printTraditionalSilent method only calls toString() on the number without assigning the result. This is a no-op operation that the JIT compiler may optimize away, making the performance comparison potentially invalid.
Consider either:
- Assigning the result to prevent optimization:
String result = number.toString(); - Using a volatile field to store results:
volatile String result;and thenresult = number.toString(); - Using a blackhole pattern if available
This ensures the work is actually performed during benchmarking.
| private static void printStreamSilent(List<Integer> numbers) { | ||
| numbers.stream().forEach(n -> n.toString()); | ||
| } |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printStreamSilent method only calls toString() on the number without using the result. This is a no-op operation that the JIT compiler may optimize away, making the performance comparison potentially invalid.
Consider either:
- Using
map(Object::toString).collect(Collectors.toList())to ensure the work is performed - Using a volatile field to accumulate results
- Using a blackhole pattern if available
This ensures the work is actually performed during benchmarking.
| 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 |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
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
| - `XxxComparison.java` - Side-by-side comparison with performance | |
| - `XxxComparison.java` - side-by-side comparison with performance |
| public class PrintNumbersTraditional { | ||
| public static void main(String[] args) { | ||
| System.out.println("Print all numbers in the list - Traditional way"); | ||
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in the numbers list initialization. For consistency and readability, add a space after the comma between '10' and '1'.
Current: ..., 9, 10,1, 2, ...
Suggested: ..., 9, 10, 1, 2, ...
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10,1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); | |
| List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10, 1, 2, 1, 3, 2, 4, 5, 6, 2, 2, 7, 8, 4, 9, 10); |
| private static void printAllNumbersInList(List<Integer> numbers) { | ||
| //it covers how to do something in a traditional way | ||
| for (Integer number : numbers) { | ||
| System.out.print(number+","); |
Copilot
AI
Dec 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after the + operator. For consistency with Java code formatting standards, add a space: number + "," instead of number+",".
This improves readability and follows common Java formatting conventions.
| System.out.print(number+","); | |
| System.out.print(number + ","); |
Print numbers with traditional approach, further with method reference, lambda expression.
Then comparing both traditional and functional approach to understand which is faster.
Traditional is faster