Skip to content

Commit 3acfa5e

Browse files
committed
feat(ForEachDemo): add demo of forEach with lambdas, method references, and custom filter
What - Added ForEachDemo class. - Created a List<Integer> with values [5,10,50,60,70,80,90]. - Demonstrated three uses of forEach(): - With a lambda expression → prints all elements. - With a method reference → prints all elements in shorter syntax. - With a custom static helper method show() → prints only elements > 60. Why - Illustrates different syntaxes supported by forEach() in Java 8+. - Demonstrates the power of method references for cleaner code. - Shows how to integrate custom filtering logic inside helper methods. How - list.forEach(n -> System.out.println(n)): - Explicit lambda parameter, prints each element. - list.forEach(System.out::println): - Method reference, shorter equivalent of the above. - list.forEach(ForEachDemo::show): - Calls custom show(int n). - show() checks if n > 60, then prints. - Prints sections with headings for clarity. Logic - Inputs: fixed list of integers. - Outputs: - Prints all values via lambda and method reference. - Prints only values > 60 via custom method. - Flow: 1. Populate list. 2. Use forEach with lambda. 3. Use forEach with method reference. 4. Use forEach with custom method. - Edge cases: - Empty list → forEach does nothing. - show() filters values, so threshold logic determines output. - Complexity: O(n) for traversal. - Concurrency: not thread-safe for concurrent modifications. Real-life applications - Iterating collections in a concise, functional style. - Method references for logging, printing, or invoking existing utilities. - Passing custom filter/transform logic into forEach for quick one-off behaviors. Notes - forEach() is a terminal operation (traverses entire collection). - Method references (::) are syntactic sugar for simple lambdas. - For filtering large datasets, prefer streams with filter() instead of custom show(). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5ea07ab commit 3acfa5e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/* Demonstrates forEach, method references, and a helper show() method. */
5+
6+
public class ForEachDemo {
7+
public static void main(String[] args) {
8+
List<Integer> list = new ArrayList<>(List.of(5, 10, 50, 60, 70, 80, 90));
9+
10+
System.out.println("forEach with lambda:");
11+
list.forEach(n -> System.out.println(n));
12+
13+
System.out.println("\nforEach with method reference:");
14+
list.forEach(System.out::println);
15+
16+
System.out.println("\nforEach with custom show() to filter (n > 60):");
17+
list.forEach(ForEachDemo::show);
18+
}
19+
private static void show(int n) {
20+
if (n > 60) {
21+
System.out.println(n);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)