← Back to Advanced Java Course | Chapter 3: Functional Programming | Lesson 2 of 6

Java के Functional Interfaces

एक functional interface एक वादा है जिसमें करने के लिए बिल्कुल एक काम है, ताकि एक lambda उसे भर सके। Predicate, Function, Consumer और Supplier तैयार-बनाई kinds हैं, जैसे standard tool shapes।
Syntax
java
@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?

java
// 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));
	}
}

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

java
// 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));
	}
}

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

java
// 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"));
	}
}

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

java
// 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());
	}
}

BiFunctional Interfaces

Java values की जोड़ियों से जुड़े cases के लिए इन core interfaces के two-argument variants भी प्रदान करता है: BiFunction दो arguments accept करता है और एक computed result return करता है, जबकि BiConsumer दो arguments accept करता है और बिना कुछ return किए एक side effect करता है।

उदाहरण: BiFunctional Interfaces

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. गलत method name call करना, जैसे predicate.apply(x); Predicate के लिए method test है, Function के लिए apply, Consumer के लिए accept और Supplier के लिए get।
  2. @FunctionalInterface चिह्नित interface में दूसरा abstract method जोड़ना, जो compile error देता है क्योंकि इसमें बिल्कुल एक होना चाहिए।
  3. यह उम्मीद करना कि एक Consumer result return करेगा, जबकि यह void return करता है; अगर आपको value वापस चाहिए तो Function का उपयोग करें।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.