Java के Functional Interfaces
In this page:
@FunctionalInterface
interface InterfaceName {
returnType methodName(parameters); // exactly one abstract method
}
InterfaceName variable = (parameters) -> expression;
What is a Functional Interface?
एक functional interface कोई भी ऐसा interface है जिसमें बिल्कुल एक abstract method हो, जो इसे किसी lambda expression या method reference के लिए एक वैध target बनाता है। आप इस पर @FunctionalInterface से annotate कर सकते हैं (और करना चाहिए), जो compiler को यह verify करने के लिए कहता है कि interface में वाकई सिर्फ एक abstract method है और अन्यथा build को fail कर दे।
उदाहरण: What is a Functional Interface?
// Define the class Main
public class Main {
@FunctionalInterface
// Define the interface Operation
interface Operation {
int apply(int a, int b);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Operation add = (a, b) -> a + b;
// Print a line to the console
System.out.println(add.apply(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
The Predicate Interface
Predicate interface एक built-in functional interface है जो एक single argument accept करता है और एक boolean return करता है, जो इसे filtering conditions के लिए standard shape बनाता है। यह java.util.function package में Java के बाकी general-purpose functional interfaces के साथ रहता है।
उदाहरण: The Predicate Interface
// Import java.util.function.Predicate so it can be used by its short name
import java.util.function.Predicate;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
// Print a line to the console
System.out.println(isEven.test(4));
// Print a line to the console
System.out.println(isEven.test(7));
}
}
Login to try C/C++/Java/PHP code in the editor
The Function Interface
Function interface एक type का single argument accept करता है और संभावित रूप से अलग type का result return करता है, input से output में एक transformation को represent करते हुए। यह आमतौर पर data conversion या mapping operations के लिए उपयोग होता है, जैसे किसी String को Integer में बदलना।
उदाहरण: The Function Interface
// Import java.util.function.Function so it can be used by its short name
import java.util.function.Function;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Function<String, Integer> toLength = s -> s.length();
// Print a line to the console
System.out.println(toLength.apply("hello"));
}
}
Login to try C/C++/Java/PHP code in the editor
Consumer and Supplier
Consumer interface एक single argument accept करता है और कोई result return नहीं करता, पूरी तरह उसके side effect के लिए किए गए operation को represent करते हुए, जैसे कोई value print करना। Supplier interface इसकी mirror image है: यह बिल्कुल कोई arguments accept नहीं करता और मांग पर सिर्फ एक generated result produce करता है।
उदाहरण: Consumer and Supplier
// Import java.util.function.Consumer so it can be used by its short name
import java.util.function.Consumer;
// Import java.util.function.Supplier so it can be used by its short name
import java.util.function.Supplier;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Consumer<String> printer = s -> System.out.println("Got: " + s);
Supplier<String> generator = () -> "generated value";
printer.accept(generator.get());
}
}
Login to try C/C++/Java/PHP code in the editor
BiFunctional Interfaces
Java values की जोड़ियों से जुड़े cases के लिए इन core interfaces के two-argument variants भी प्रदान करता है: BiFunction दो arguments accept करता है और एक computed result return करता है, जबकि BiConsumer दो arguments accept करता है और बिना कुछ return किए एक side effect करता है।
उदाहरण: BiFunctional Interfaces
// Import java.util.function.BiFunction so it can be used by its short name
import java.util.function.BiFunction;
// Import java.util.function.BiConsumer so it can be used by its short name
import java.util.function.BiConsumer;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
BiConsumer<String, Integer> log = (name, age) -> System.out.println(name + " is " + age);
// Print a line to the console
System.out.println(multiply.apply(3, 4));
log.accept("Sam", 30);
}
}
Login to try C/C++/Java/PHP code in the editor
- गलत method name call करना, जैसे
predicate.apply(x); Predicate के लिए methodtestहै, Function के लिएapply, Consumer के लिएacceptऔर Supplier के लिएget। @FunctionalInterfaceचिह्नित interface में दूसरा abstract method जोड़ना, जो compile error देता है क्योंकि इसमें बिल्कुल एक होना चाहिए।- यह उम्मीद करना कि एक Consumer result return करेगा, जबकि यह
voidreturn करता है; अगर आपको value वापस चाहिए तो Function का उपयोग करें।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: