-
Notifications
You must be signed in to change notification settings - Fork 0
new: added filtering even numbers #4
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 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
ComparisonUtilsutility class withrepeat()andformatTime()helper methods - Implemented Q002 (Print Even Numbers) with traditional, stream, and comparison approaches
- Refactored Q001 to use the new
ComparisonUtilsinstead 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.
| 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 + ",")); |
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.
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.
| 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 + ",")); |
| } | ||
|
|
||
| private static void printNumbersWithLambda(Stream<Integer> integerStream) { | ||
| integerStream.forEach(n-> System.out.println(n + ",")); |
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.
[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 + \",\").
| integerStream.forEach(n-> System.out.println(n + ",")); | |
| integerStream.forEach(n -> System.out.println(n + ",")); |
| - **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) |
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 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).
| - **Q002: Sum of Squares** - filter(), map(), reduce() (INCOMPLETE - needs Stream version) | |
| - **Q002: Print Even Numbers** - filter(), forEach() (COMPLETE) |
| ## Planned Problems | ||
|
|
||
| ### Easy | ||
| - Q003: Print even numbers only |
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.
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.
| - Q003: Print even numbers only | |
| - Q003: Print odd numbers only |
| for (Integer number : numbers) { | ||
| if(number % 2 == 0) { | ||
| // 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.
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.
| private static void printStreamSilent(List<Integer> numbers) { | ||
| numbers.stream() | ||
| .filter(n -> n % 2 == 0) | ||
| .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.
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.
| 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); |
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.
[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.
| 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 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); |
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.
[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.
| 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 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); |
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.
[nitpick] Missing space after comma before 1 (between 10,1). Add space for consistent formatting.
| 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); |
No description provided.