Skip to content

Commit 1d2acb6

Browse files
committed
feat(JAVA8): add demo combining lambdas, streams, and default interface methods
What - Added JAVA8 class implementing Greetable interface. - Greetable defines default method greet(String name) → prints greeting. - Demonstrates multiple Java 8 features: - Lambda expression with forEach to print list of names. - Stream API: - Filtering even numbers and summing them. - Filtering names starting with "C" and collecting results. - Default method in interface called through JAVA8 instance. Why - Provides consolidated example of Java 8 functional programming and interface enhancements. - Shows how lambdas reduce boilerplate for iteration. - Highlights stream transformations and aggregations with filter, mapToInt, sum, and collect. - Demonstrates backward-compatible interface evolution with default methods. How - Created list of names ["Alice","Bob","Charlie"]. - Used forEach with lambda to print each name. - Created list of numbers [1–6]. - Built stream pipeline: - filter even numbers → [2,4,6]. - mapToInt → [2,4,6]. - sum → 12. - Built another pipeline: - filter names starting with "C" → ["Charlie"]. - collect into new list. - Instantiated JAVA8 class and invoked greet("Developer"). Logic - Inputs: - List<String> names = ["Alice","Bob","Charlie"]. - List<Integer> numbers = [1,2,3,4,5,6]. - Outputs: - Printed names line by line. - Printed "Sum of even numbers: 12". - Printed "Names starting with 'C': [Charlie]". - Printed "Hello, Developer!". - Flow: 1. Lambda prints list elements. 2. Stream sums even integers. 3. Stream filters names by prefix. 4. greet() prints default method message. - Edge cases: - Empty lists → no output from streams. - greet() works for any non-null string. - Complexity / performance: O(n) per stream pipeline. - Concurrency / thread-safety: Lists are immutable (Arrays.asList). Stream operations stateless and safe here. - Error handling: None required. Real-life applications - Printing collections with lambdas instead of loops. - Summing filtered values (e.g., transactions, scores). - Filtering data by conditions and collecting into lists. - Using default methods to add behavior to interfaces without breaking implementations. Notes - Method references (System.out::println) could simplify lambda in forEach. - Streams are lazy; execution triggered by terminal ops like sum() or collect(). - Default methods help evolve interfaces with minimal disruption. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7c28c51 commit 1d2acb6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
import java.util.stream.Collectors;
3+
4+
interface Greetable {
5+
// Default method in interface
6+
default void greet(String name) {
7+
System.out.println("Hello, " + name + "!");
8+
}
9+
}
10+
11+
public class JAVA8 implements Greetable {
12+
public static void main(String[] args) {
13+
// Lambda Expression Example
14+
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
15+
16+
System.out.println("Using Lambda to print names:");
17+
names.forEach(name -> System.out.println(name));
18+
19+
// Stream API Example
20+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
21+
22+
int sumOfEven = numbers.stream()
23+
.filter(n -> n % 2 == 0)
24+
.mapToInt(Integer::intValue)
25+
.sum();
26+
27+
System.out.println("\nSum of even numbers: " + sumOfEven);
28+
29+
List<String> filteredNames = names.stream()
30+
.filter(n -> n.startsWith("C"))
31+
.collect(Collectors.toList());
32+
33+
System.out.println("Names starting with 'C': " + filteredNames);
34+
35+
// Default Method Example
36+
JAVA8 demo = new JAVA8();
37+
demo.greet("Developer");
38+
}
39+
}

0 commit comments

Comments
 (0)