Skip to content

Commit 0f1dfba

Browse files
committed
Updated FunctionalInterfacesExample with comments and JavaDocs
1 parent f31925d commit 0f1dfba

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

functionalInterfaces/FunctionalInterfacesExample.java

+37-40
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,44 @@
99

1010
/**
1111
* The FunctionalInterfacesExample class demonstrates the use of built-in
12-
* functional interfaces in Java such as Consumer, Predicate, Function, and Supplier.
12+
* functional interfaces in Java such as Consumer, Predicate, Function, and
13+
* Supplier.
1314
*/
1415
public class FunctionalInterfacesExample {
1516

16-
/**
17-
* The main method serves as the entry point for the program. It demonstrates
18-
* how to use various functional interfaces to manipulate a list of names.
19-
*
20-
* @param args command-line arguments (not used)
21-
*/
22-
public static void main(String[] args) {
23-
// Creating a list of names
24-
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
25-
26-
// Consumer: Print each name
27-
// A Consumer takes a single argument and performs an operation without returning a result.
28-
Consumer<String> printName = name -> System.out.println("Name: " + name);
29-
System.out.println("Using Consumer:");
30-
names.forEach(printName); // Applying Consumer to print each name
31-
32-
// Predicate: Filter names that start with the letter 'A'
33-
// A Predicate takes a single argument and returns a boolean result.
34-
Predicate<String> startsWithA = name -> name.startsWith("A");
35-
System.out.println("\nNames starting with 'A':");
36-
names.stream()
37-
.filter(startsWithA) // Filtering names based on the Predicate
38-
.forEach(printName); // Applying Consumer to print filtered names
39-
40-
// Function: Convert names to their lengths
41-
// A Function takes one argument and returns a result.
42-
Function<String, Integer> nameLength = name -> name.length();
43-
System.out.println("\nLengths of names:");
44-
names.stream()
45-
.map(nameLength) // Mapping names to their lengths using the Function
46-
.forEach(length -> System.out.println("Length: " + length));
47-
48-
// Supplier: Generate a default name
49-
// A Supplier takes no arguments and returns a result.
50-
Supplier<String> defaultNameSupplier = () -> "swaraj";
51-
System.out.println("\nGenerated name from Supplier: " + defaultNameSupplier.get()); // Calling Supplier to get a default value
52-
}
17+
/**
18+
* The main method serves as the entry point for the program. It demonstrates
19+
* how to use various functional interfaces to manipulate a list of names.
20+
*/
21+
public static void main(String[] args) {
22+
// Creating a list of names
23+
List<String> names = Arrays.asList("Swaraj", "Ram", "Santosh", "Djsai");
24+
25+
// Consumer: Print each name
26+
// A Consumer takes a single argument and performs an operation without
27+
// returning a result.
28+
Consumer<String> printName = name -> System.out.println("Name: " + name);
29+
System.out.println("Using Consumer:");
30+
names.forEach(printName); // Applying Consumer to print each name
31+
32+
// Predicate: Filter names that start with the letter 'S'
33+
// A Predicate takes a single argument and returns a boolean result.
34+
Predicate<String> startsWithS = name -> name.startsWith("S");
35+
System.out.println("\nNames starting with 'S':");
36+
names.stream().filter(startsWithS) // Filtering names based on the Predicate
37+
.forEach(printName); // Applying Consumer to print filtered names
38+
39+
// Function: Convert names to their lengths
40+
// A Function takes one argument and returns a result.
41+
Function<String, Integer> nameLength = name -> name.length();
42+
System.out.println("\nLengths of names:");
43+
names.stream().map(nameLength) // Mapping names to their lengths using the Function
44+
.forEach(length -> System.out.println("Length: " + length));
45+
46+
// Supplier: Generate a default name
47+
// A Supplier takes no arguments and returns a result.
48+
Supplier<String> defaultNameSupplier = () -> "Swaraj";
49+
System.out.println("\nGenerated name from Supplier: " + defaultNameSupplier.get()); // Calling Supplier to get a
50+
// default value
51+
}
5352
}
54-
55-

streamPipeline/StreamPipelineExample.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44
import java.util.List;
55
import java.util.stream.Collectors;
66

7+
/**
8+
* A class that demonstrates the use of Java Streams to process a list of names.
9+
* It shows how to filter, transform, and collect the results using a stream pipeline.
10+
*/
711
public class StreamPipelineExample {
8-
public static void main(String[] args) {
9-
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
12+
/**
13+
* The main method serves as the entry point of the program.
14+
* It demonstrates how to use a stream pipeline to filter and transform a list of names.
15+
*
16+
* @param args Command-line arguments (not used).
17+
*/
18+
public static void main(String[] args) {
19+
List<String> names = Arrays.asList("Swaraj", "Ram", "Santosh", "Djsai");
1020

1121
List<String> filteredNames = names.stream()
12-
.filter(name -> name.startsWith("A")) // Intermediate operation
13-
.map(String::toUpperCase) // Intermediate operation
14-
.collect(Collectors.toList()); // Terminal operation
22+
.filter(name -> name.startsWith("R")) // Intermediate operation( The 'filter' method is used to retain only those names that start with the letter 'A'.)
23+
.map(String::toUpperCase) // Intermediate operation(The 'map' method transforms the filtered names to uppercase.)
24+
.collect(Collectors.toList()); // Terminal operation(The 'collect' method gathers the results into a new list.)
1525

16-
System.out.println(filteredNames); // Output: [ALICE]
26+
// Output the filtered and transformed names
27+
System.out.println(filteredNames); // Output: [RAM]
1728
}
1829
}

0 commit comments

Comments
 (0)