-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
80 lines (55 loc) · 3.13 KB
/
Test.java
File metadata and controls
80 lines (55 loc) · 3.13 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
import java.util.function.*;
import java.lang.Runnable;
import java.awt.event.ActionListener;
import java.util.Comparator;
public class Test {
//Functional Interface
// A functional interface is an interface that has only one abstract method. It can have multiple
// default and static methods.
// Example of a functional interface
@FunctionalInterface
interface MyFunctionalInterface {
void myMethod();
}
public static void main(String[] args) {
// Predicate Interface
// A predicate is a functional interface that takes one argument and returns a boolean value.
Predicate<Integer> isEven = num -> num % 2 == 0;
System.out.println("Is 4 even? " + isEven.test(4));
// Consumer Interface
// A consumer is a functional interface that takes one argument and returns no result.
Consumer<String> printMessage = message -> System.out.println(message);
printMessage.accept("Hello, World!");
//BinaryOperator Interface
// A binary operator is a functional interface that takes two arguments of the same type and returns
// a result of the same type.
BinaryOperator<Integer> add = (a, b) -> a + b;
System.out.println("Sum of 5 and 10: " + add.apply(5, 10));
//Runnable is a functional interface that has only one abstract method run()
Runnable myRunnable = () -> System.out.println("Running in a thread");
Thread thread = new Thread(myRunnable);
thread.start();
//Comparable is a functional interface that has only one abstract method compareTo()
Comparator<Integer> myComparator = (a, b) -> a.compareTo(b);
System.out.println("Comparing 5 and 10: " + myComparator.compare(5, 10));
// Action Listener is a functional interface that has only one abstract method actionPerformed()
// Example of ActionListener
ActionListener myActionListener = e -> System.out.println("Button clicked");
// Supplier is a functional interface that has only one abstract method get()
// Example of Supplier
Supplier<String> mySupplier = () -> "Hello, World!";
System.out.println("Supplier result: " + mySupplier.get());
// BiConsumer is a functional interface that takes two arguments and returns no result.
// Example of BiConsumer
BiConsumer<String, Integer> myBiConsumer = (name, age) -> System.out.println(name + " is " + age + " years old");
myBiConsumer.accept("Alice", 30);
// BiFunction is a functional interface that takes two arguments and returns a result.
// Example of BiFunction
BiFunction<Integer, Integer, Integer> myBiFunction = (a, b) -> a + b;
System.out.println("BiFunction result: " + myBiFunction.apply(5, 10));
//UnaryOperator is a functional interface that takes one argument and returns a result of the same type.
// Example of UnaryOperator
UnaryOperator<Integer> myUnaryOperator = a -> a * a;
System.out.println("UnaryOperator result: " + myUnaryOperator.apply(5));
}
}