-
Notifications
You must be signed in to change notification settings - Fork 0
new: added map and max min methods from stream apis #5
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 two new Stream API learning examples (Q003: Sum of Squares and Q004: Max & Min) to the Java 8 educational module, completing the initial set of four problems. Each problem follows the established pattern of providing Traditional, Stream, and Comparison implementations to help learners understand the differences between imperative and functional programming styles in Java 8.
Key Changes
- Q003 Sum of Squares: Demonstrates
map()transformation andsum()terminal operations for calculating sum of squared values - Q004 Max & Min: Shows
max()andmin()terminal operations withOptionalhandling for finding extreme values - Utility enhancements: Added helper methods for printing arrays and generating random test data for performance comparisons
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
ComparisonUtils.java |
Added printNumbers() and getArrayOfSpecifiedSize() utility methods for array operations and test data generation |
Pair.java |
New helper class to encapsulate min/max result pairs |
MaxMinNumberTraditional.java |
Traditional single-pass approach to find max and min values |
MaxMinNumberStream.java |
Stream API implementation using max() and min() with Optional handling |
MaxMinNumberComparison.java |
Performance benchmarks comparing both approaches across multiple dataset sizes |
SumOfSquareOfNumberTraditional.java |
Traditional for-loop approach to sum squared values |
SumOfSquareOfNumberStream.java |
Stream API using map() and sum() for functional transformation |
SumOfSquareOfNumberComparison.java |
Side-by-side performance comparison with timing metrics |
SumOfSquareOfNumber.java |
Empty placeholder class removed |
README.md |
Comprehensive updates including project structure, build instructions, IDE setup, troubleshooting, and current status |
PROBLEMS.md |
Added detailed documentation for Q003 and Q004 with examples, concepts covered, and performance notes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| */ | ||
| public static void printNumbers(int[] numbers) { | ||
| for (Integer number : numbers) { | ||
| System.out.print(number+","); |
Copilot
AI
Dec 3, 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 number before the + operator. Should be number + "," for consistency with Java style conventions.
| System.out.print(number+","); | |
| System.out.print(number + ","); |
|
|
||
| /** | ||
| * Print the elements of an integer array. | ||
| * @param numbers |
Copilot
AI
Dec 3, 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 Javadoc comment is missing the @param description text. It should include a description like @param numbers the array of integers to print.
| * @param numbers | |
| * @param numbers the array of integers to print |
| * method to generate an array of specified size with random integers. | ||
| * @param size | ||
| * @return |
Copilot
AI
Dec 3, 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 Javadoc is incomplete. Both @param size and @return tags are missing their descriptions. Should include descriptions like:
@param size the number of elements in the array@return an array of random integers in the range [-100, 100]
| * method to generate an array of specified size with random integers. | |
| * @param size | |
| * @return | |
| * Method to generate an array of specified size with random integers. | |
| * @param size the number of elements in the array | |
| * @return an array of random integers in the range [-100, 100] |
| * method to generate an array of specified size with random integers. | ||
| * @param size | ||
| * @return | ||
| */ | ||
| public static int[] getArrayOfSpecifiedSize( int size) { |
Copilot
AI
Dec 3, 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] Method naming could be improved. The name getArrayOfSpecifiedSize is verbose. Consider a more concise name like generateRandomArray or createRandomIntArray which better conveys the method's purpose.
| * method to generate an array of specified size with random integers. | |
| * @param size | |
| * @return | |
| */ | |
| public static int[] getArrayOfSpecifiedSize( int size) { | |
| * Generate an array of specified size with random integers. | |
| * @param size the size of the array to generate | |
| * @return an array of random integers | |
| */ | |
| public static int[] generateRandomIntArray(int size) { |
| public void setMinElement(int minElement) { | ||
| this.minElement = minElement; | ||
| } | ||
|
|
||
| public int getMaxElement() { | ||
| return maxElement; | ||
| } | ||
|
|
||
| public void setMaxElement(int maxElement) { | ||
| this.maxElement = maxElement; | ||
| } | ||
|
|
Copilot
AI
Dec 3, 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] Consider making the Pair class immutable by removing the setters (setMinElement() and setMaxElement()). Since this class represents a result value that shouldn't change after creation, immutability would be more appropriate and prevent accidental modifications.
| public void setMinElement(int minElement) { | |
| this.minElement = minElement; | |
| } | |
| public int getMaxElement() { | |
| return maxElement; | |
| } | |
| public void setMaxElement(int maxElement) { | |
| this.maxElement = maxElement; | |
| } | |
| public int getMaxElement() { | |
| return maxElement; | |
| } |
| @@ -0,0 +1,29 @@ | |||
| package com.modernjava.guide.java8.Q004_max_min; | |||
|
|
|||
|
|
|||
Copilot
AI
Dec 3, 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 class is missing a class-level Javadoc comment. Since this is a new public class used as a data holder, it should have documentation explaining its purpose, e.g., "Represents a pair of minimum and maximum integer values."
| /** | |
| * Represents a pair of minimum and maximum integer values. | |
| */ |
| public class Pair { | ||
| private int minElement; | ||
| private int maxElement; | ||
|
|
||
| public Pair(int minElement, int maxElement) { |
Copilot
AI
Dec 3, 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] The class name Pair is too generic and could be confused with utility Pair classes from other libraries. Consider a more descriptive name like MinMaxPair or MinMaxResult that clearly indicates it holds min/max values.
| public class Pair { | |
| private int minElement; | |
| private int maxElement; | |
| public Pair(int minElement, int maxElement) { | |
| public class MinMaxPair { | |
| private int minElement; | |
| private int maxElement; | |
| public MinMaxPair(int minElement, int maxElement) { |
|
|
||
| public class MaxMinNumberComparison { | ||
| public static void main(String[] args) { | ||
| int[] numbers = {45, 22, 89, 11, 67, 34, 90, 5,-121, 342, 0, 9999, -5000}; |
Copilot
AI
Dec 3, 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 array initialization. Should be {45, 22, 89, 11, 67, 34, 90, 5, -121, 342, 0, 9999, -5000} for consistent formatting.
| int[] numbers = {45, 22, 89, 11, 67, 34, 90, 5,-121, 342, 0, 9999, -5000}; | |
| int[] numbers = {45, 22, 89, 11, 67, 34, 90, 5, -121, 342, 0, 9999, -5000}; |
| * 2. Iterate through the array starting from the second element. | ||
| * 3. Update max if the current element is greater than max. | ||
| * 4. Update min if the current element is less than min. | ||
| */ |
Copilot
AI
Dec 3, 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 empty array validation. The method will throw ArrayIndexOutOfBoundsException if numbers is empty when accessing numbers[0] on lines 28-29. Consider adding a check like:
if (numbers == null || numbers.length == 0) {
return new Pair(0, 0); // or throw IllegalArgumentException
}This would make the behavior consistent with the Stream approach which handles empty arrays using orElse(0).
| */ | |
| */ | |
| if (numbers == null || numbers.length == 0) { | |
| return new Pair(0, 0); | |
| } |
| * @param numbers | ||
| */ | ||
| public static void printNumbers(int[] numbers) { | ||
| for (Integer number : numbers) { |
Copilot
AI
Dec 3, 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 variable 'number' is only assigned values of primitive type and is never 'null', but it is declared with the boxed type 'Integer'.
| for (Integer number : numbers) { | |
| for (int number : numbers) { |
Q003_square_nums/ # ✅ Sum of squares
Q004_max_min/ # ✅ Find max & min