Skip to content

Conversation

@durgesh137
Copy link
Owner

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

Copilot AI review requested due to automatic review settings December 1, 2025 18:14
@durgesh137 durgesh137 merged commit aa4dec2 into main Dec 1, 2025
8 checks passed
Copy link

Copilot AI left a 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);
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.

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, ...

Suggested change
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);

Copilot uses AI. Check for mistakes.
}

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);
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.

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, ...

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment on lines +172 to +175
private static void printTraditionalSilent(List<Integer> numbers) {
for (Integer number : numbers) {
// No output for performance testing
number.toString();
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.

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:

  1. Assigning the result to prevent optimization: String result = number.toString();
  2. Using a volatile field to store results: volatile String result; and then result = number.toString();
  3. Using a blackhole pattern if available

This ensures the work is actually performed during benchmarking.

Copilot uses AI. Check for mistakes.
Comment on lines +182 to +184
private static void printStreamSilent(List<Integer> numbers) {
numbers.stream().forEach(n -> n.toString());
}
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.

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:

  1. Using map(Object::toString).collect(Collectors.toList()) to ensure the work is performed
  2. Using a volatile field to accumulate results
  3. Using a blackhole pattern if available

This ensures the work is actually performed during benchmarking.

Copilot uses AI. Check for mistakes.
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.
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);
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.

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, ...

Suggested change
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);

Copilot uses AI. Check for mistakes.
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+",");
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.

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.

Suggested change
System.out.print(number+",");
System.out.print(number + ",");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants