Tuple তৈরি করার বিভিন্ন পদ্ধতি

Tuple Construction এবং Accessor Methods - জাভা টাপল (Java Tuples) - Java Technologies

340

জাভায় Tuple হল একটি ডেটা স্ট্রাকচার যা একাধিক ভিন্ন বা একই ধরনের ডেটা একত্রে সংরক্ষণ করতে ব্যবহৃত হয়। জাভায় বিল্ট-ইন Tuple সমর্থন নেই, তবে বিভিন্ন third-party libraries এবং custom implementation ব্যবহার করে Tuple তৈরি করা যায়। Tuple তৈরি করার পদ্ধতিগুলো নিচে বিস্তারিতভাবে আলোচনা করা হলো।


১. Third-Party Libraries ব্যবহার করে Tuple তৈরি

(১) Vavr Library ব্যবহার করা

Vavr একটি জনপ্রিয় লাইব্রেরি যা Immutable Tuples তৈরি করতে সহায়ক।

Installation (Maven):

<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.10.4</version>
</dependency>

Example: Tuple Creation with Vavr

import io.vavr.Tuple;
import io.vavr.Tuple3;

public class VavrTupleExample {
    public static void main(String[] args) {
        Tuple3<String, Integer, String> person = Tuple.of("Alice", 30, "Engineer");

        System.out.println("Name: " + person._1);  // Alice
        System.out.println("Age: " + person._2);   // 30
        System.out.println("Profession: " + person._3); // Engineer
    }
}

(২) Apache Commons Lang ব্যবহার করা

Apache Commons Lang Pair এবং Triple টাপল সরবরাহ করে।

Installation (Maven):

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Example: Tuple Creation with Apache Commons Lang

import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;

public class ApacheTupleExample {
    public static void main(String[] args) {
        Pair<String, Integer> pair = Pair.of("Bob", 25);
        System.out.println("Name: " + pair.getLeft());  // Bob
        System.out.println("Age: " + pair.getRight()); // 25

        Triple<String, Integer, String> triple = Triple.of("Carol", 28, "Designer");
        System.out.println("Name: " + triple.getLeft());    // Carol
        System.out.println("Age: " + triple.getMiddle());  // 28
        System.out.println("Profession: " + triple.getRight()); // Designer
    }
}

(৩) Javatuples Library ব্যবহার করা

Javatuples বিভিন্ন ধরনের Tuple (যেমন, Pair, Triplet, Quartet, Decade) সমর্থন করে।

Installation (Maven):

<dependency>
    <groupId>org.javatuples</groupId>
    <artifactId>javatuples</artifactId>
    <version>1.2</version>
</dependency>

Example: Tuple Creation with Javatuples

import org.javatuples.Pair;
import org.javatuples.Triplet;

public class JavatuplesExample {
    public static void main(String[] args) {
        Pair<String, Integer> pair = new Pair<>("Dave", 35);
        System.out.println("Name: " + pair.getValue0());  // Dave
        System.out.println("Age: " + pair.getValue1());   // 35

        Triplet<String, Integer, String> triplet = new Triplet<>("Eve", 40, "Architect");
        System.out.println("Name: " + triplet.getValue0());    // Eve
        System.out.println("Age: " + triplet.getValue1());     // 40
        System.out.println("Profession: " + triplet.getValue2()); // Architect
    }
}

২. Custom Implementation ব্যবহার করে Tuple তৈরি

Example: Generic Tuple Class

class Tuple2<T1, T2> {
    public final T1 first;
    public final T2 second;

    public Tuple2(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }
}

public class CustomTupleExample {
    public static void main(String[] args) {
        Tuple2<String, Integer> person = new Tuple2<>("John", 30);

        System.out.println("Name: " + person.first); // John
        System.out.println("Age: " + person.second); // 30
    }
}

Example: Tuple Class Supporting Multiple Values

class Tuple<T1, T2, T3> {
    public final T1 first;
    public final T2 second;
    public final T3 third;

    public Tuple(T1 first, T2 second, T3 third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }
}

public class MultiValueTupleExample {
    public static void main(String[] args) {
        Tuple<String, Integer, String> person = new Tuple<>("Alice", 28, "Engineer");

        System.out.println("Name: " + person.first);      // Alice
        System.out.println("Age: " + person.second);     // 28
        System.out.println("Profession: " + person.third); // Engineer
    }
}

৩. Java Record ব্যবহার করে Tuple তৈরি (Java 14+)

Java 14+ এ Record ব্যবহার করে সহজেই Immutable Tuples তৈরি করা যায়।

Example:

public record Pair<T1, T2>(T1 first, T2 second) {}

public class RecordTupleExample {
    public static void main(String[] args) {
        Pair<String, Integer> pair = new Pair<>("Eve", 32);

        System.out.println("Name: " + pair.first()); // Eve
        System.out.println("Age: " + pair.second()); // 32
    }
}

৪. Map.Entry ব্যবহার করে Tuple তৈরি (Java Built-in)

Java এর Map.Entry ইন্টারফেস দ্বৈত মান সংরক্ষণের জন্য ব্যবহার করা যায়।

Example:

import java.util.AbstractMap;

public class MapEntryExample {
    public static void main(String[] args) {
        AbstractMap.SimpleEntry<String, Integer> entry = new AbstractMap.SimpleEntry<>("John", 30);

        System.out.println("Name: " + entry.getKey());   // John
        System.out.println("Age: " + entry.getValue()); // 30
    }
}

Tuple তৈরির পদ্ধতির তুলনা

পদ্ধতিবৈশিষ্ট্যউপযুক্ত ব্যবহার
VavrImmutable Tuples, 1-8 Elements।জাভা প্রজেক্টে Immutable Data ব্যবহার।
Apache Commons Langসহজ Pair এবং Triple সমর্থন।Simple Key-Value এবং Triple Data স্টোর।
Javatuples1-10 Elements পর্যন্ত সমর্থন।Complex Tuple Requirements।
Custom Implementationনির্দিষ্ট প্রয়োজন অনুযায়ী Tuple কাস্টমাইজেশন।বিশেষ Tuple ফিচার প্রয়োজন হলে।
Java RecordImmutable এবং Readable।Java 14+ প্রজেক্টের জন্য।
Map.EntryBuilt-in Java API দ্বারা Key-Value সংরক্ষণ।Key-Value স্টোরেজ এবং Data Transfer।

  1. Third-Party Libraries (যেমন Vavr, Apache Commons Lang, এবং Javatuples) সহজ এবং শক্তিশালী সমাধান প্রদান করে।
  2. Custom Implementation নির্দিষ্ট প্রয়োজন অনুযায়ী Tuple তৈরি করতে কার্যকর।
  3. Java Records ব্যবহার করলে Tuple তৈরি আরো সহজ এবং Immutable হয়।
  4. Map.Entry সহজ Key-Value পেয়ারের জন্য বিল্ট-ইন সাপোর্ট দেয়।

আপনার প্রয়োজন অনুযায়ী পদ্ধতি নির্বাচন করলে Tuple তৈরি ও ব্যবহারে উন্নত অভিজ্ঞতা পাওয়া যাবে।

Content added By
Promotion

Are you sure to start over?

Loading...