-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdasBasics.java
More file actions
103 lines (78 loc) · 3.29 KB
/
Copy pathLambdasBasics.java
File metadata and controls
103 lines (78 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package functional;
import java.util.function.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
https://www.geeksforgeeks.org/java/lambda-expressions-java-8/
* Lambda expressions and functional interfaces
*/
public final class LambdasBasics {
private static final Logger LOGGER = Logger.getLogger(LambdasBasics.class.getName());
// Private constructor to prevent instantiation
private LambdasBasics() {
throw new IllegalStateException("Utility class");
}
// Main method to demonstrate Lambda expressions
public static void main() {
LOGGER.info("--- Starting Lambda Expressions Demonstration ---");
demonstrateRunnable();
demonstrateFunction();
demonstrateBiFunction();
demonstratePredicate();
demonstrateSupplier();
demonstrateMultiLineLambda();
demonstrateMethodReference();
LOGGER.info("--- Demonstration Completed ---");
}
// Runnable: Accepts no parameters, returns no value
private static void demonstrateRunnable() {
LOGGER.info("--- Runnable ---");
Runnable task = () -> LOGGER.info("Running a task with no parameters.");
task.run();
}
// IntUnaryOperator: Specialized to accept an int and return an int
private static void demonstrateFunction() {
LOGGER.info("--- Function ---");
IntUnaryOperator square = n -> n * n;
LOGGER.log(Level.INFO, "5 squared: {0}", square.applyAsInt(5));
}
// IntBinaryOperator: Accepts two primitive ints and returns a primitive int (No Integer boxing)
private static void demonstrateBiFunction() {
LOGGER.info("--- BiFunction ---");
IntBinaryOperator multiply = (a, b) -> a * b;
LOGGER.log(Level.INFO, "4 x 6 = {0}", multiply.applyAsInt(4, 6));
}
// IntPredicate: Accepts a primitive int and returns a boolean
private static void demonstratePredicate() {
LOGGER.info("--- Predicate ---");
IntPredicate isEven = n -> n % 2 == 0;
LOGGER.log(Level.INFO, "Is 7 even?: {0}", isEven.test(7));
LOGGER.log(Level.INFO, "Is 8 even?: {0}", isEven.test(8));
}
// Supplier<T>: Accepts no parameters, returns a value of type T
private static void demonstrateSupplier() {
LOGGER.info("--- Supplier ---");
Supplier<String> messageSupplier = () -> "Dynamically generated message";
LOGGER.log(Level.INFO, "{0}", messageSupplier.get());
}
// IntFunction<String>: Accepts a primitive int and returns an Object (String)
private static void demonstrateMultiLineLambda() {
LOGGER.info("--- Multi-line Lambda ---");
IntFunction<String> classifyNumber = n -> {
if (n < 0) {
return "Negative";
}
if (n == 0) {
return "Zero";
}
return "Positive";
};
LOGGER.log(Level.INFO, "Classification of -5: {0}", classifyNumber.apply(-5));
}
// ToIntFunction<String>: Accepts an Object (String) and returns a primitive int
private static void demonstrateMethodReference() {
LOGGER.info("--- Method Reference ---");
ToIntFunction<String> stringLength = String::length;
LOGGER.log(Level.INFO, "Length of 'Java': {0}", stringLength.applyAsInt("Java"));
}
}