|
9 | 9 |
|
10 | 10 | /**
|
11 | 11 | * 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. |
13 | 14 | */
|
14 | 15 | public class FunctionalInterfacesExample {
|
15 | 16 |
|
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 | + } |
53 | 52 | }
|
54 |
| - |
55 |
| - |
0 commit comments