Skip to content

Conversation

@durgesh137
Copy link
Owner

No description provided.

Copilot AI review requested due to automatic review settings December 1, 2025 19:01
@durgesh137 durgesh137 merged commit aff9fbe 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 adds filtering and printing of even numbers functionality by implementing Q002 problem set, creating a reusable utility class, and refactoring existing code to use it. The PR adds four new Java files and updates three documentation files.

Key Changes

  • Created ComparisonUtils utility class with repeat() and formatTime() helper methods
  • Implemented Q002 (Print Even Numbers) with traditional, stream, and comparison approaches
  • Refactored Q001 to use the new ComparisonUtils instead of local methods

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
ComparisonUtils.java New utility class providing string repeat and time formatting helpers for comparison classes
PrintEvensTraditional.java Traditional for-loop implementation for filtering and printing even numbers
PrintEvensStream.java Stream API implementation demonstrating filter operation with even numbers
PrintEvensComparison.java Comparison runner with performance benchmarks for traditional vs stream approaches
PrintNumbersComparison.java Refactored to use ComparisonUtils methods instead of local implementations
SumOfSquareOfNumber.java Empty placeholder class for Q003
README.md Updated with documentation references and quick commands
PROBLEMS.md New file documenting completed and planned problems
PROBLEM_TEMPLATE.md Removed duplicate problem list that now exists in PROBLEMS.md

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

Comment on lines +19 to +33
Stream<Integer> integerStream = numbers.stream()
.filter(n -> n % 2 == 0);//filter even numbers

//filter even numbers stream is acquired, now can be printed either using method reference or lambda
printNumbersWithLambda(integerStream);
printNumsWithMethodReference(integerStream);

}

private static void printNumsWithMethodReference(Stream<Integer> integerStream) {
integerStream.forEach(System.out::println);
}

private static void printNumbersWithLambda(Stream<Integer> integerStream) {
integerStream.forEach(n-> System.out.println(n + ","));
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.

Stream can only be consumed once. The second call to printNumsWithMethodReference(integerStream) will throw IllegalStateException: stream has already been operated upon or closed because the stream was already consumed by printNumbersWithLambda(). Create a new stream for each operation or remove one of the method calls.

Suggested change
Stream<Integer> integerStream = numbers.stream()
.filter(n -> n % 2 == 0);//filter even numbers
//filter even numbers stream is acquired, now can be printed either using method reference or lambda
printNumbersWithLambda(integerStream);
printNumsWithMethodReference(integerStream);
}
private static void printNumsWithMethodReference(Stream<Integer> integerStream) {
integerStream.forEach(System.out::println);
}
private static void printNumbersWithLambda(Stream<Integer> integerStream) {
integerStream.forEach(n-> System.out.println(n + ","));
// Each method gets its own stream to avoid IllegalStateException
printNumbersWithLambda(numbers);
printNumsWithMethodReference(numbers);
}
private static void printNumsWithMethodReference(List<Integer> numbers) {
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
}
private static void printNumbersWithLambda(List<Integer> numbers) {
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(n -> System.out.println(n + ","));

Copilot uses AI. Check for mistakes.
}

private static void printNumbersWithLambda(Stream<Integer> integerStream) {
integerStream.forEach(n-> System.out.println(n + ","));
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.

[nitpick] Inconsistent spacing around lambda arrow operator. There should be a space before and after ->, not multiple spaces after. Change to n -> System.out.println(n + \",\").

Suggested change
integerStream.forEach(n-> System.out.println(n + ","));
integerStream.forEach(n -> System.out.println(n + ","));

Copilot uses AI. Check for mistakes.
- **Sum of Squares of Odd Numbers**: Combining filter, map, and reduce operations
- More coming soon...
- **Q001: Print Numbers** - forEach(), method references, lambda expressions (WORKING)
- **Q002: Sum of Squares** - filter(), map(), reduce() (INCOMPLETE - needs Stream version)
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 README describes Q002 as 'Sum of Squares', but the actual implementation in this PR is 'Print Even Numbers'. Update the description to match the actual Q002 implementation: - **Q002: Print Even Numbers** - filter(), forEach() (COMPLETE).

Suggested change
- **Q002: Sum of Squares** - filter(), map(), reduce() (INCOMPLETE - needs Stream version)
- **Q002: Print Even Numbers** - filter(), forEach() (COMPLETE)

Copilot uses AI. Check for mistakes.
## Planned Problems

### Easy
- Q003: Print even numbers only
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.

Q003 is listed as 'Print even numbers only' in the Planned Problems section, but Q002 already implements printing even numbers. This appears to be a duplicate. Update to a different problem or clarify the distinction.

Suggested change
- Q003: Print even numbers only
- Q003: Print odd numbers only

Copilot uses AI. Check for mistakes.
for (Integer number : numbers) {
if(number % 2 == 0) {
// 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.

Calling toString() on a statement by itself has no effect and will be optimized away by the compiler. For accurate performance testing, assign the result to a volatile variable or use a method like Objects.requireNonNull() to ensure the operation isn't eliminated.

Copilot uses AI. Check for mistakes.
private static void printStreamSilent(List<Integer> numbers) {
numbers.stream()
.filter(n -> n % 2 == 0)
.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.

Calling toString() within forEach has no side effects and may be optimized away. For accurate performance testing, use a volatile variable to store results or use a method that ensures the operation isn't eliminated by the JIT compiler.

Copilot uses AI. Check for mistakes.
public class PrintEvensTraditional {
public static void main(String[] args) {
System.out.println("Print Even 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.

[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.

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 class PrintEvensStream {
public static void main(String[] args) {
System.out.println("Print all Even 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.

[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.

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 class PrintEvensComparison {

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.

[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.

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