-
Notifications
You must be signed in to change notification settings - Fork 0
new: added searching substrings with string[] with lambdas #6
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||||||||||
| package com.modernjava.guide.java8.Q005_print_string; | ||||||||||||||
|
|
||||||||||||||
| import com.modernjava.guide.java8.util.ComparisonUtils; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Q005: Print all strings containing a specific substring | ||||||||||||||
| * Compares Traditional approach vs Stream API approach | ||||||||||||||
| * Tests with small, medium, and large datasets | ||||||||||||||
| */ | ||||||||||||||
| public class PrintStringsComparison { | ||||||||||||||
| public static void main(String[] args) { | ||||||||||||||
| String substring = "_test"; | ||||||||||||||
|
|
||||||||||||||
| System.out.println("Q005: Print All Strings containing substring '_test' using Both Approaches"); | ||||||||||||||
| System.out.println(ComparisonUtils.repeat("=", 80)); | ||||||||||||||
|
|
||||||||||||||
| // Test Case 1: Small dataset (20 strings) | ||||||||||||||
| System.out.println("\n" + ComparisonUtils.repeat("-", 30) + " Test Case 1: Small Dataset " + ComparisonUtils.repeat("-", 30)); | ||||||||||||||
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(20, substring); | ||||||||||||||
| System.out.println("Input (size=" + strings.length + "): "); | ||||||||||||||
| ComparisonUtils.printStrings(strings); | ||||||||||||||
| compareApproaches(strings, substring, 20); | ||||||||||||||
|
|
||||||||||||||
| // Test Case 2: Medium dataset (1,000 strings) | ||||||||||||||
| System.out.println("\n\n" + ComparisonUtils.repeat("-", 30) + " Test Case 2: Medium Dataset " + ComparisonUtils.repeat("-", 30)); | ||||||||||||||
| strings = ComparisonUtils.getStringArrayWithPatternAtEnd(1_000, substring); | ||||||||||||||
| System.out.println("Input (size=" + strings.length + "): "); | ||||||||||||||
| System.out.println("[... 1,000 strings ...]"); | ||||||||||||||
| compareApproaches(strings, substring, 1_000); | ||||||||||||||
|
|
||||||||||||||
| // Test Case 3: Large dataset (100,000 strings) | ||||||||||||||
| System.out.println("\n\n" + ComparisonUtils.repeat("-", 30) + " Test Case 3: Large Dataset " + ComparisonUtils.repeat("-", 30)); | ||||||||||||||
| strings = ComparisonUtils.getStringArrayWithPatternAtEnd(100_000, substring); | ||||||||||||||
| System.out.println("Input (size=" + strings.length + "): "); | ||||||||||||||
| System.out.println("[... 100,000 strings ...]"); | ||||||||||||||
| compareApproaches(strings, substring, 100_000); | ||||||||||||||
|
|
||||||||||||||
| // Test Case 4: Very Large dataset (1,000,000 strings) | ||||||||||||||
| System.out.println("\n\n" + ComparisonUtils.repeat("-", 30) + " Test Case 4: Very Large Dataset " + ComparisonUtils.repeat("-", 30)); | ||||||||||||||
| strings = ComparisonUtils.getStringArrayWithPatternAtEnd(1_000_000, substring); | ||||||||||||||
| System.out.println("Input (size=" + strings.length + "): "); | ||||||||||||||
| System.out.println("[... 1,000,000 strings ...]"); | ||||||||||||||
| compareApproaches(strings, substring, 1_000_000); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private static void compareApproaches(String[] strings, String substring, int size) { | ||||||||||||||
| // Traditional Approach | ||||||||||||||
| System.out.println("\nAPPROACH: Traditional"); | ||||||||||||||
| System.out.println(ComparisonUtils.repeat("-", 80)); | ||||||||||||||
| long startTime = System.nanoTime(); | ||||||||||||||
|
|
||||||||||||||
| if (size > 50) { | ||||||||||||||
| System.out.println("[Skipped printing output for large array of size " + size + "]"); | ||||||||||||||
| } else { | ||||||||||||||
| PrintStringsTraditional.printStrings(strings, substring); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| long endTime = System.nanoTime(); | ||||||||||||||
| long traditionalTime = endTime - startTime; | ||||||||||||||
| System.out.println("Time: " + traditionalTime + " ns (" + ComparisonUtils.formatTime(traditionalTime) + ")"); | ||||||||||||||
|
|
||||||||||||||
| // Stream API Approach | ||||||||||||||
| System.out.println("\nAPPROACH: Stream API"); | ||||||||||||||
| System.out.println(ComparisonUtils.repeat("-", 80)); | ||||||||||||||
| long streamStartTime = System.nanoTime(); | ||||||||||||||
| PrintStringsStream.searchStrings(strings, substring); | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
Comment on lines
+66
to
+68
|
||||||||||||||
| PrintStringsStream.searchStrings(strings, substring); | |
| if (size > 50) { | |
| System.out.println("[Skipped printing output for large array of size " + size + "]"); | |
| } else { | |
| PrintStringsStream.searchStrings(strings, substring); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||||||||||||
| package com.modernjava.guide.java8.Q005_print_string; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import com.modernjava.guide.java8.util.ComparisonUtils; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * search strings containing a substring using Stream API | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| public class PrintStringsStream { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||
| System.out.println("Q005: Print All Strings containing substring '_test' using Stream API"); | ||||||||||||||||||||||
| ComparisonUtils.repeat("-",80);; | ||||||||||||||||||||||
|
||||||||||||||||||||||
| ComparisonUtils.repeat("-",80);; | |
| ComparisonUtils.repeat("-",80); |
Copilot
AI
Dec 4, 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 method arguments. Should be getStringArrayWithPatternAtEnd(40, "_test") with a space after the comma for consistency with Java code style conventions.
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(40,"_test"); | |
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(40, "_test"); |
Copilot
AI
Dec 4, 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 + operator. Should be "Input with length " + strings.length + ": " with spaces around the concatenation operators for consistency.
| System.out.println("Input with length "+strings.length+": "); | |
| System.out.println("Input with length " + strings.length + ": "); |
Copilot
AI
Dec 4, 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.
Method naming inconsistency. In PrintStringsStream.java, the method is named searchStrings() while in PrintStringsTraditional.java, it's named printStrings(). Both methods have the same purpose (filtering and printing strings with a substring). For consistency and clarity, they should have the same name. Consider using printStringsWithSubstring() or filterAndPrintStrings() for both.
| searchStrings(strings, subString); | |
| } | |
| public static void searchStrings(String[] strings, String subString){ | |
| printStringsWithSubstring(strings, subString); | |
| } | |
| public static void printStringsWithSubstring(String[] strings, String subString){ |
Copilot
AI
Dec 4, 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 in lambda expression arrow operator. Should be .filter(n -> n.contains(subString)) with spaces around the arrow operator for consistency with Java lambda style conventions.
| .filter(n->n.contains(subString)) | |
| .forEach(n->System.out.print(n+",")); | |
| .filter(n -> n.contains(subString)) | |
| .forEach(n -> System.out.print(n+",")); |
Copilot
AI
Dec 4, 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 in lambda expression arrow operator. Should be .forEach(n -> System.out.print(n+",")) with spaces around the arrow operator for consistency with Java lambda style conventions.
| .filter(n->n.contains(subString)) | |
| .forEach(n->System.out.print(n+",")); | |
| .filter(n -> n.contains(subString)) | |
| .forEach(n -> System.out.print(n+",")); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||||||||||||||||||
| package com.modernjava.guide.java8.Q005_print_string; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import com.modernjava.guide.java8.util.ComparisonUtils; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public class PrintStringsTraditional { | ||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||
| String substring = "_test"; | ||||||||||||||||||||||
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(20,substring); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(20,substring); | |
| String[] strings = ComparisonUtils.getStringArrayWithPatternAtEnd(20, substring); |
Copilot
AI
Dec 4, 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.
Double semicolon detected. Remove the extra semicolon at the end of this line.
| System.out.println(ComparisonUtils.repeat("-",80));; | |
| System.out.println(ComparisonUtils.repeat("-",80)); |
Copilot
AI
Dec 4, 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.
Misleading documentation comment. The comment states "print all strings in an array" but the method actually prints only strings containing the specified substring. Update the comment to accurately reflect the method's behavior: "Traditional approach to print all strings containing the specified substring."
| * Traditional approach to print all strings in an array. | |
| * Steps: | |
| * 1. Use a for-each loop to iterate through each string in the array. | |
| * 2. Print each string to the console. | |
| * Traditional approach to print all strings containing the specified substring. | |
| * Steps: | |
| * 1. Use a for-each loop to iterate through each string in the array. | |
| * 2. If the string contains the specified substring, print it to the console. |
Copilot
AI
Dec 4, 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.
Misleading documentation comment. Step 2 states "Print each string to the console" but the method actually prints only strings that contain the specified substring. Update to: "2. Print strings that contain the substring to the console."
| * 2. Print each string to the console. | |
| * 2. Print strings that contain the substring to the console. |
Copilot
AI
Dec 4, 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 if keyword. Should be if (str.contains(substring)) with a space before the opening parenthesis, following Java style conventions.
| if(str.contains(substring)) | |
| if (str.contains(substring)) |
Copilot
AI
Dec 4, 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 braces for if statement. According to Java best practices, single-line if statements should include braces for better readability and maintainability. Should be:
if(str.contains(substring)) {
System.out.print(str+",");
}| if(str.contains(substring)) | |
| System.out.print(str+","); | |
| if(str.contains(substring)) { | |
| System.out.print(str+","); | |
| } |
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.
Duplicate entry "Q014: Partition list into even/odd" appears on both line 177 and line 178. Remove one of these duplicate entries.