// Java program to demonstrate functional interface class Test { public static void main(String args[]) { new Thread(new Runnable() { @Override public void run() { System.out.println("New Thread "); } }).start(); } }
Output:
New Thread
class Test { public static void main(String args[]) { new Thread(() -> { // lambda expression System.out.println("New Thread "); }).start(); } }
Output:
New Thread
To ensure that the functional interface can only have one abstract method, the @FunctionalInterface annotation is used. An "Unexpected @FunctionalInterface annotation" message is flagged by the compiler if there are multiple abstract methods present. It is not necessary to use this annotation, though.
@FunctionalInterface interface Operation { int operate(int a, int b); } public class Calculator { public static void main(String[] args) { Operation add = (a, b) -> a + b; Operation subtract = (a, b) -> a - b; Operation multiply = (a, b) -> a * b; int result1 = calculate(5, 2, add); int result2 = calculate(5, 2, subtract); int result3 = calculate(5, 2, multiply); System.out.println(result1); // prints 7 System.out.println(result2); // prints 3 System.out.println(result3); // prints 10 } public static int calculate(int a, int b, Operation op) { return op.operate(a, b); } }
Output:
7
3
10
In this example, we define a functional interface called Operation with a single method called operate that takes two integers and returns an integer. We use the @FunctionalInterface annotation to indicate that this interface is intended to be used as a functional interface.
We then define three instances of the Operation interface using lambda expressions for addition, subtraction, and multiplication. We pass these instances to the calculate method, which takes two integers and an instance of the Operation interface, and returns the result of applying the operation to the integers.
When we run the program, we get the results of calculating the sum, difference, and product of 5 and 2, using the different operations.
Java offers predefined functional interfaces that use lambda and method references to deal with functional programming. Additionally, you can create a unique functional interface. Here is a list of the functional interfaces found in the java.util.function package.
Consumer<String> printStr = s -> System.out.println(s); printStr.accept("Hello World!"); // Output: "Hello World!"
BiConsumer<String, Integer> printInfo = (name, age) -> System.out.println(name + " is " + age + " years old."); printInfo.accept("John", 30); // Output: "John is 30 years old."
Supplier<Integer> getRandomNumber = () -> (int) (Math.random() * 100); int num = getRandomNumber.get(); // Returns a random number between 0 and 100
Function<String, Integer> getStrLength = s -> s.length(); int length = getStrLength.apply("Hello"); // Returns 5
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b; int sum = add.apply(2, 3); // Returns 5
UnaryOperator<Integer> square = x -> a * a; int num = square.apply(5); // Returns 25
BinaryOperator<Integer> add = (a, b) -> a + b; int sum = add.apply(2, 3); // Returns 5
Predicate<Integer> isEven = x -> x % 2 == 0; boolean result = isEven.test(4); // Returns true
BiPredicate<Integer, Integer> isSumEven = (a, b) -> (a + b) % 2 == 0; boolean result = isSumEven.test(2, 3); // Returns true
ToIntFunction<String> getStrLength = s -> s.length(); int length = getStrLength.applyAsInt("Hello"); // Returns 5
ToLongFunction<String> getStrHash = s -> s.hashCode(); long hash = getStrHash.applyAsLong("Hello"); // Returns a hash value of the string
ToDoubleFunction<String> getStrLength = s -> (double) s.length(); double length = getStrLength.applyAsDouble("Hello"); // Returns 5.0
IntFunction<String> getStrWithLength = length -> "The length of this string is " + length; String str = getStrWithLength.apply(10); // Returns "The length of this string is 10"
LongFunction<String> getStrWithHash = hash -> "The hash value of this string is " + hash; String str = getStrWithHash.apply("Hello".hashCode()); // Returns "The hash value of this string is <hash_value>"
DoubleFunction<String> getStrWithSquare = x -> "The square of " + x + " is " + (x * x); String str = getStrWithSquare.apply(5.0); // Returns "The square of 5.0 is 25.0"
IntSupplier getRandomNumber = () -> (int) (Math.random() * 100); int num = getRandomNumber.getAsInt(); // Returns a random number between 0 and 100
LongSupplier getCurrentTime = () -> System.currentTimeMillis(); long time = getCurrentTime.getAsLong(); // Returns the current time in milliseconds
DoubleSupplier getRandomDouble = () -> Math.random(); double num = getRandomDouble.getAsDouble(); // Returns a random double value between 0.0 and 1.0
IntPredicate isEven = x -> x % 2 == 0; boolean result = isEven.test(4); // Returns true
LongPredicate isPositive = x -> x > 0; boolean result = isPositive.test(10L); // Returns true
DoublePredicate isGreaterThanOne = x -> x > 1.0; boolean result = isGreaterThanOne.test(2.0); // Returns true
Police Colony
Patna, Bihar
India
Email:
Post your comment