BiFunction এবং BiPredicate

Built-in Functional Interfaces - জাভা ফাংশনাল প্রোগ্রামিং (Java Functional Programming) - Java Technologies

319

Java 8 থেকে Functional Programming ধারণা Java-তে যুক্ত হয়েছে এবং এর মধ্যে Functional Interfaces একটি গুরুত্বপূর্ণ ভূমিকা পালন করে। এই ইন্টারফেসগুলি lambda expressions এবং method references এর মাধ্যমে ফাংশনাল প্রোগ্রামিংয়ের সুবিধা প্রদান করে। দুইটি গুরুত্বপূর্ণ Functional Interface হল BiFunction এবং BiPredicate। এই দুটি ইন্টারফেস ব্যবহার করে আপনি দুইটি ইনপুট নিয়ে বিভিন্ন কার্যকরী এবং শর্তপূর্ণ ফাংশন তৈরি করতে পারেন।

এখানে BiFunction এবং BiPredicate ইন্টারফেসের ধারণা, ব্যবহার এবং উদাহরণ দেওয়া হয়েছে।


1. BiFunction Interface

BiFunction একটি functional interface যা দুইটি ইনপুট প্যারামিটার গ্রহণ করে এবং একটি আউটপুট প্রদান করে। এর মধ্যে দুটি ইনপুট প্যারামিটার থাকে এবং একটি রিটার্ন ভ্যালু থাকে।

BiFunction Interface-এর Syntax

@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}
  • T: প্রথম ইনপুট প্যারামিটার টাইপ।
  • U: দ্বিতীয় ইনপুট প্যারামিটার টাইপ।
  • R: রিটার্ন টাইপ।

BiFunction Interface-এর ব্যবহার

BiFunction ব্যবহার করা হয় যখন আপনি দুটি ইনপুট প্যারামিটার গ্রহণ করে এবং তাদের উপর কোনো কাজ করেন এবং একটি ফলাফল প্রদান করেন। এটি সাধারণত lambda expression বা method reference এর মাধ্যমে ব্যবহার করা হয়।

BiFunction Example:

ধরা যাক, একটি BiFunction ব্যবহার করে দুটি সংখ্যার যোগফল বের করা:

import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        // BiFunction to add two numbers
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

        // Call the BiFunction
        int result = add.apply(10, 20);
        System.out.println("Sum: " + result);  // Output: Sum: 30
    }
}

এখানে, BiFunction<Integer, Integer, Integer> দুটি ইনপুট গ্রহণ করছে (প্রথম এবং দ্বিতীয় ইনপুট হল Integer) এবং একটি Integer রিটার্ন করছে (যোগফল)।


2. BiPredicate Interface

BiPredicate একটি functional interface যা দুটি ইনপুট প্যারামিটার গ্রহণ করে এবং একটি boolean মান রিটার্ন করে। এটি শর্ত যাচাই করার জন্য ব্যবহৃত হয়, যেমন predicate

BiPredicate Interface-এর Syntax

@FunctionalInterface
public interface BiPredicate<T, U> {
    boolean test(T t, U u);
}
  • T: প্রথম ইনপুট প্যারামিটার টাইপ।
  • U: দ্বিতীয় ইনপুট প্যারামিটার টাইপ।

BiPredicate Interface-এর ব্যবহার

BiPredicate সাধারণত এমন শর্ত যাচাই করতে ব্যবহৃত হয়, যেখানে দুটি প্যারামিটার নেওয়া হয় এবং ফলস্বরূপ true বা false রিটার্ন করা হয়। এটি সাধারণত lambda expression বা method reference এর মাধ্যমে ব্যবহৃত হয়।

BiPredicate Example:

ধরা যাক, আমরা একটি BiPredicate ব্যবহার করে দুটি সংখ্যার মধ্যে চেক করব যে, প্রথমটি দ্বিতীয়টির চেয়ে বড় কিনা:

import java.util.function.BiPredicate;

public class Main {
    public static void main(String[] args) {
        // BiPredicate to check if the first number is greater than the second
        BiPredicate<Integer, Integer> isGreater = (a, b) -> a > b;

        // Call the BiPredicate
        boolean result = isGreater.test(10, 5);
        System.out.println("Is 10 greater than 5? " + result);  // Output: true
    }
}

এখানে, BiPredicate<Integer, Integer> দুটি Integer ইনপুট গ্রহণ করছে এবং একটি boolean রিটার্ন করছে, যা যাচাই করছে প্রথম সংখ্যা দ্বিতীয় সংখ্যার চেয়ে বড় কিনা।


3. BiFunction এবং BiPredicate এর মধ্যে পার্থক্য

FeatureBiFunctionBiPredicate
PurposeAccepts two parameters and returns a result (any type).Accepts two parameters and returns a boolean result.
Return TypeCan return any type of value.Returns a boolean value.
Use CaseUsed for operations that produce a result from two inputs.Used for operations that test a condition on two inputs.
Common OperationsArithmetic operations, string concatenation, transformations.Checking conditions like equality, greater than, less than, etc.

4. Chaining with BiFunction and BiPredicate

Chaining BiFunction:

BiFunction ইনপুট নিয়ে একটি আউটপুট প্রদান করে, এবং আপনি একাধিক BiFunction একত্রে চেইন করতে পারেন। andThen() বা compose() মেথড ব্যবহার করে আপনি একাধিক BiFunction একটি সাথে সংযুক্ত করতে পারেন।

import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;

        // Chain BiFunctions
        BiFunction<Integer, Integer, Integer> result = add.andThen(multiply);

        // The result is first added and then multiplied
        System.out.println(result.apply(2, 3));  // Output: 15 (2+3=5, 5*3=15)
    }
}

এখানে andThen() মেথডটি প্রথমে যোগফল হিসাব করে তারপর গুণফল বের করে।

Chaining BiPredicate:

BiPredicate এর সাথে আপনি and(), or(), এবং negate() মেথড ব্যবহার করে শর্তগুলি চেইন করতে পারেন।

import java.util.function.BiPredicate;

public class Main {
    public static void main(String[] args) {
        BiPredicate<Integer, Integer> isGreaterThan = (a, b) -> a > b;
        BiPredicate<Integer, Integer> isEqual = (a, b) -> a == b;

        // Chaining BiPredicates
        BiPredicate<Integer, Integer> combinedPredicate = isGreaterThan.and(isEqual);

        // Check if 10 is greater than 5 and equal to 5
        System.out.println(combinedPredicate.test(10, 5));  // Output: false
    }
}

এখানে, and() মেথড দুটি শর্ত একত্রে ব্যবহার করেছে, যেখানে প্রথম শর্তে 10 > 5 সঠিক হলেও দ্বিতীয় শর্তে 10 == 5 ভুল হওয়ার কারণে ফলস্বরূপ false পাওয়া যায়।


সারাংশ

  • BiFunction একটি functional interface যা দুইটি ইনপুট প্যারামিটার গ্রহণ করে এবং একটি আউটপুট রিটার্ন করে।
  • BiPredicate একটি functional interface যা দুইটি ইনপুট প্যারামিটার গ্রহণ করে এবং একটি boolean রিটার্ন করে।
  • BiFunction সাধারণত গাণিতিক বা ট্রান্সফর্মেশন অপারেশন পরিচালনা করতে ব্যবহৃত হয়, যখন BiPredicate শর্ত যাচাই করতে ব্যবহৃত হয়।
  • Lambda expressions এবং method references ব্যবহার করে সহজেই BiFunction এবং BiPredicate এর কার্যকরী ব্যবহার করা যায়।

এই দুটি ইন্টারফেস Java Functional Programming এর গুরুত্বপূর্ণ অংশ, এবং তাদের মাধ্যমে আপনি জাভাতে আরও কার্যকরী এবং কমপ্যাক্ট কোড লিখতে পারবেন।

Content added By
Promotion

Are you sure to start over?

Loading...