Tuple Construction এবং Accessor Methods

জাভা টাপল (Java Tuples) - Java Technologies

396

Javatuples লাইব্রেরি জাভাতে Tuples এর জন্য একটি শক্তিশালী সমাধান। এটি বিভিন্ন ধরনের Tuples সরবরাহ করে, যেমন Pair, Triplet, Quartet, ইত্যাদি। Tuples হল Immutable ডেটা স্ট্রাকচার, যা একাধিক ডেটা টাইপের মান একসাথে সংরক্ষণ করে।


Tuple Construction (Tuple তৈরি করা)

১. Pair তৈরি করা (2 উপাদান যুক্ত Tuple):

import org.javatuples.Pair;

public class TupleExample {
    public static void main(String[] args) {
        // Pair তৈরি করা
        Pair<String, Integer> person = Pair.with("Alice", 30);
        System.out.println(person);
    }
}

আউটপুট:

[Alice, 30]

২. Triplet তৈরি করা (3 উপাদান যুক্ত Tuple):

import org.javatuples.Triplet;

public class TupleExample {
    public static void main(String[] args) {
        // Triplet তৈরি করা
        Triplet<String, Integer, String> student = Triplet.with("Bob", 20, "Mathematics");
        System.out.println(student);
    }
}

আউটপুট:

[Bob, 20, Mathematics]

৩. Quartet তৈরি করা (4 উপাদান যুক্ত Tuple):

import org.javatuples.Quartet;

public class TupleExample {
    public static void main(String[] args) {
        // Quartet তৈরি করা
        Quartet<String, Integer, String, Boolean> employee = Quartet.with("John", 35, "Manager", true);
        System.out.println(employee);
    }
}

আউটপুট:

[John, 35, Manager, true]

৪. অন্যান্য Tuples:

  • Quintet (5 উপাদান), Sextet (6 উপাদান), Septet (7 উপাদান), ইত্যাদি।

Accessor Methods (Tuple এর মান অ্যাক্সেস করা)

১. getValueX() পদ্ধতি ব্যবহার করা

  • প্রতিটি Tuple ক্লাসে getValueX() মেথড ব্যবহার করে নির্দিষ্ট মান অ্যাক্সেস করা যায়, যেখানে X হলো মানের অবস্থান (0 থেকে শুরু)।

উদাহরণ:

import org.javatuples.Triplet;

public class AccessorExample {
    public static void main(String[] args) {
        Triplet<String, Integer, String> student = Triplet.with("Alice", 25, "Physics");

        // Accessor Methods
        String name = student.getValue0();
        Integer age = student.getValue1();
        String subject = student.getValue2();

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Subject: " + subject);
    }
}

আউটপুট:

Name: Alice
Age: 25
Subject: Physics

২. addAtX() পদ্ধতি ব্যবহার করে মান যোগ করা

  • একটি নতুন Tuple তৈরি করে যেখানে নির্দিষ্ট স্থানে একটি মান যোগ করা হয়।

উদাহরণ:

import org.javatuples.Pair;

public class AddExample {
    public static void main(String[] args) {
        Pair<String, Integer> person = Pair.with("Alice", 30);

        // নতুন Tuple তৈরি করে
        Triplet<String, Integer, String> newPerson = person.addAt2("Doctor");
        System.out.println(newPerson);
    }
}

আউটপুট:

[Alice, 30, Doctor]

৩. removeFromX() পদ্ধতি ব্যবহার করে মান সরানো

  • নির্দিষ্ট স্থানে থাকা মান সরিয়ে একটি নতুন Tuple তৈরি করা যায়।

উদাহরণ:

import org.javatuples.Triplet;

public class RemoveExample {
    public static void main(String[] args) {
        Triplet<String, Integer, String> student = Triplet.with("Alice", 25, "Physics");

        // নতুন Tuple তৈরি করে
        Pair<String, Integer> newStudent = student.removeFrom2();
        System.out.println(newStudent);
    }
}

আউটপুট:

[Alice, 25]

৪. setAtX() পদ্ধতি ব্যবহার করে মান পরিবর্তন

  • নির্দিষ্ট স্থানে নতুন মান সেট করে একটি নতুন Tuple তৈরি করা হয়।

উদাহরণ:

import org.javatuples.Triplet;

public class SetExample {
    public static void main(String[] args) {
        Triplet<String, Integer, String> student = Triplet.with("Alice", 25, "Physics");

        // নতুন Tuple তৈরি করে
        Triplet<String, Integer, String> updatedStudent = student.setAt2("Mathematics");
        System.out.println(updatedStudent);
    }
}

আউটপুট:

[Alice, 25, Mathematics]

Javatuples এর কিছু গুরুত্বপূর্ণ ফিচার

ফিচারবর্ণনা
Immutable StructureTuples পরিবর্তনযোগ্য নয়, যা ডেটা সুরক্ষিত রাখে।
Type SafetyTuples টাইপ-সেফ, তাই ভুল ডেটা টাইপ ব্যবহার করা সম্ভব নয়।
Various Sizes১ থেকে ১০ উপাদান সমর্থনকারী Tuples প্রদান করে।
Utility MethodsaddAtX(), removeFromX(), এবং setAtX() এর মতো মেথড।

Javatuples ব্যবহার করে Tuple তৈরি এবং অ্যাক্সেস করা সহজ এবং কার্যকর। এটি Immutable এবং Type-Safe ডেটা স্ট্রাকচার প্রদান করে, যা জাভাতে একাধিক মান সংরক্ষণ এবং পরিচালনার জন্য একটি নির্ভরযোগ্য সমাধান। Accessor Methods যেমন getValueX(), addAtX(), এবং setAtX() ব্যবহার করে Tuples কে আরও কার্যকরভাবে ব্যবহার করা যায়।

Content added By

জাভায় 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

Tuple হলো একাধিক ডেটা ধরন (data type) সংরক্ষণ করার জন্য ব্যবহৃত একটি ডেটা স্ট্রাকচার। এটি বিভিন্ন টাইপের মান একত্রে একটি ইউনিট হিসেবে সংরক্ষণ করতে সাহায্য করে। জাভা নিজস্বভাবে Tuple সরবরাহ করে না, তবে Apache Commons Lang, Vavr, বা কাস্টম ক্লাস ব্যবহার করে Tuple ইমপ্লিমেন্ট করা যায়।


Tuple কীভাবে বিভিন্ন ডেটা ধরন সংরক্ষণ করে?

  1. Tuple একাধিক মান (values) সংরক্ষণ করতে পারে, যেখানে প্রতিটি মান ভিন্ন ডেটা টাইপের হতে পারে।
  2. এটি immutable হয়, অর্থাৎ Tuple তৈরি হলে তা পরিবর্তন করা যায় না।
  3. Tuple বিভিন্ন ডেটা টাইপ সংরক্ষণে সহায়ক।

Tuple ব্যবহার করে বিভিন্ন ডেটা ধরন সংরক্ষণের উদাহরণ

১. Apache Commons Lang লাইব্রেরি ব্যবহার করে

Dependency (Maven):

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

কোড উদাহরণ:

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

public class ApacheCommonsTupleExample {
    public static void main(String[] args) {
        // Pair: দুইটি ডেটা সংরক্ষণ
        Pair<String, Integer> student = Pair.of("Alice", 20);
        System.out.println("Name: " + student.getLeft());
        System.out.println("Age: " + student.getRight());

        // Triple: তিনটি ডেটা সংরক্ষণ
        Triple<String, Integer, String> employee = Triple.of("Bob", 30, "Engineer");
        System.out.println("Name: " + employee.getLeft());
        System.out.println("Age: " + employee.getMiddle());
        System.out.println("Role: " + employee.getRight());
    }
}

২. Vavr লাইব্রেরি ব্যবহার করে

Dependency (Maven):

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

কোড উদাহরণ:

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

public class VavrTupleExample {
    public static void main(String[] args) {
        // Tuple3: তিনটি ডেটা সংরক্ষণ
        Tuple3<String, Double, Boolean> product = Tuple.of("Laptop", 999.99, true);

        System.out.println("Product Name: " + product._1); // _1 -> প্রথম ডেটা
        System.out.println("Price: $" + product._2);      // _2 -> দ্বিতীয় ডেটা
        System.out.println("Available: " + product._3);   // _3 -> তৃতীয় ডেটা
    }
}

৩. Custom Tuple Class তৈরি

কোড উদাহরণ:

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

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

    public T1 getFirst() {
        return first;
    }

    public T2 getSecond() {
        return second;
    }

    public T3 getThird() {
        return third;
    }
}

public class CustomTupleExample {
    public static void main(String[] args) {
        // Custom Tuple ব্যবহার করে ডেটা সংরক্ষণ
        Tuple<String, Integer, Boolean> data = new Tuple<>("John", 25, true);

        System.out.println("Name: " + data.getFirst());
        System.out.println("Age: " + data.getSecond());
        System.out.println("Enrolled: " + data.getThird());
    }
}

Tuple ব্যবহার করে ডেটা সংরক্ষণের ক্ষেত্র

১. মাল্টিপল রিটার্ন ভ্যালু

একটি মেথড একাধিক ভ্যালু ফিরিয়ে দিতে পারে।

import io.vavr.Tuple;
import io.vavr.Tuple2;

public class MultipleReturnExample {
    public static Tuple2<Integer, Integer> calculate(int a, int b) {
        return Tuple.of(a + b, a * b);
    }

    public static void main(String[] args) {
        Tuple2<Integer, Integer> result = calculate(5, 10);
        System.out.println("Sum: " + result._1);
        System.out.println("Product: " + result._2);
    }
}

২. ডেটা পাসিং

একাধিক ডেটা টাইপ একত্রে পাস করা।

import io.vavr.Tuple3;

public class DataPassingExample {
    public static void main(String[] args) {
        Tuple3<String, Integer, Boolean> user = Tuple.of("Alice", 25, true);
        printUserInfo(user);
    }

    public static void printUserInfo(Tuple3<String, Integer, Boolean> user) {
        System.out.println("Name: " + user._1);
        System.out.println("Age: " + user._2);
        System.out.println("Active: " + user._3);
    }
}

৩. ডেটা গ্রুপিং

সমান টাইপের ডেটা একত্রিত করে সংরক্ষণ করা।

import io.vavr.Tuple2;

public class DataGroupingExample {
    public static void main(String[] args) {
        Tuple2<String, String> coordinates = Tuple.of("23.5N", "45.3E");
        System.out.println("Latitude: " + coordinates._1);
        System.out.println("Longitude: " + coordinates._2);
    }
}

Tuple ব্যবহার করার সুবিধা

  1. Lightweight Structure: বিভিন্ন টাইপের ডেটা সংরক্ষণের জন্য সহজ।
  2. Immutable: ডেটা নিরাপদ থাকে কারণ এটি পরিবর্তন করা যায় না।
  3. Reusability: Tuple ক্লাস একাধিক জায়গায় পুনরায় ব্যবহার করা যায়।
  4. Multiple Return Values: একটি মেথড সহজেই একাধিক মান ফিরিয়ে দিতে পারে।

Tuple ব্যবহার করার সীমাবদ্ধতা

  1. নামবিহীন ডেটা অ্যাক্সেস: ডেটা অ্যাক্সেস করার সময় _1, _2 ইত্যাদি ব্যবহার করতে হয়, যা কোড কম বোধগম্য করতে পারে।
  2. Complex Data Structure: জটিল ডেটার জন্য POJO ক্লাস বেশি কার্যকর।
  3. Standard Library: জাভার নিজস্ব লাইব্রেরিতে Tuple নেই; তৃতীয় পক্ষের লাইব্রেরি ব্যবহার করতে হয়।

Tuple একটি কার্যকর ডেটা স্ট্রাকচার, যা বিভিন্ন ডেটা টাইপ একত্রে সংরক্ষণ এবং পরিচালনা করার জন্য খুবই উপযোগী। এটি মাল্টিপল রিটার্ন ভ্যালু এবং লাইটওয়েট ডেটা পাসিংয়ের জন্য আদর্শ। Apache Commons Lang, Vavr, অথবা কাস্টম Tuple ক্লাস ব্যবহার করে জাভায় Tuples ইমপ্লিমেন্ট করা যায়।

আপনার নির্দিষ্ট প্রয়োজন অনুযায়ী Tuple এর ধরন এবং পদ্ধতি নির্বাচন করুন।

Content added By

Java Tuples একাধিক ভিন্ন ডেটা টাইপের মান একত্রে সংরক্ষণ করতে ব্যবহৃত হয়। Tuples থেকে উপাদান (elements) অ্যাক্সেস করার বিভিন্ন কৌশল রয়েছে। এখানে বিভিন্ন লাইব্রেরি ব্যবহার করে এবং কাস্টম Tuples থেকে উপাদান অ্যাক্সেস করার পদ্ধতি নিয়ে আলোচনা করা হলো।


১. Javatuples লাইব্রেরি ব্যবহার করে Elements Access

Javatuples লাইব্রেরি থেকে Tuples থেকে উপাদান অ্যাক্সেস করার জন্য বিভিন্ন পদ্ধতি সরবরাহ করে, যেমন getValue(index) এবং নির্দিষ্ট ভ্যালু অ্যাক্সেস করার জন্য getValueX()

Quartet থেকে উপাদান অ্যাক্সেস

import org.javatuples.Quartet;

public class AccessTupleElements {
    public static void main(String[] args) {
        // Quartet তৈরি
        Quartet<String, Integer, Double, Boolean> data = 
                new Quartet<>("Alice", 25, 3.75, true);

        // উপাদান অ্যাক্সেস করার বিভিন্ন পদ্ধতি
        String name = data.getValue0(); // প্রথম উপাদান
        int age = data.getValue1();    // দ্বিতীয় উপাদান
        double gpa = data.getValue2(); // তৃতীয় উপাদান
        boolean graduated = data.getValue3(); // চতুর্থ উপাদান

        // আউটপুট
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("GPA: " + gpa);
        System.out.println("Graduated: " + graduated);
    }
}

আউটপুট:

Name: Alice
Age: 25
GPA: 3.75
Graduated: true

Generic পদ্ধতি: getValue(index) ব্যবহার করে অ্যাক্সেস

getValue(index) একটি জেনেরিক পদ্ধতি যা টাপল থেকে যে কোনো উপাদান অ্যাক্সেস করতে পারে।

কোড উদাহরণ:

import org.javatuples.Triplet;

public class GenericAccessExample {
    public static void main(String[] args) {
        // Triplet তৈরি
        Triplet<String, Integer, String> data = 
                new Triplet<>("John", 30, "Engineer");

        // Generic Access
        System.out.println("First Element: " + data.getValue(0)); // John
        System.out.println("Second Element: " + data.getValue(1)); // 30
        System.out.println("Third Element: " + data.getValue(2)); // Engineer
    }
}

আউটপুট:

First Element: John
Second Element: 30
Third Element: Engineer

২. Vavr লাইব্রেরি ব্যবহার করে Elements Access

Vavr লাইব্রেরি টাপল থেকে উপাদান অ্যাক্সেস করার জন্য _1, _2, _3 ইত্যাদি ব্যবহার করে। এটি Tuples এর 8 পর্যন্ত মান সমর্থন করে।

কোড উদাহরণ:

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

public class VavrTupleExample {
    public static void main(String[] args) {
        // Tuple তৈরি
        Tuple3<String, Integer, String> data = Tuple.of("Alice", 28, "Doctor");

        // উপাদান অ্যাক্সেস
        String name = data._1; // প্রথম উপাদান
        int age = data._2;     // দ্বিতীয় উপাদান
        String profession = data._3; // তৃতীয় উপাদান

        // আউটপুট
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Profession: " + profession);
    }
}

আউটপুট:

Name: Alice
Age: 28
Profession: Doctor

৩. কাস্টম Tuples থেকে Elements Access

কাস্টম Tuples তৈরি করে সরাসরি গেটার মেথড দিয়ে উপাদান অ্যাক্সেস করা যায়।

কোড উদাহরণ:

public class CustomTuple<A, B, C> {
    private final A first;
    private final B second;
    private final C third;

    public CustomTuple(A first, B second, C third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public A getFirst() {
        return first;
    }

    public B getSecond() {
        return second;
    }

    public C getThird() {
        return third;
    }
}

public class CustomTupleExample {
    public static void main(String[] args) {
        // Custom Tuple তৈরি
        CustomTuple<String, Integer, Boolean> data = 
                new CustomTuple<>("Bob", 35, true);

        // উপাদান অ্যাক্সেস
        System.out.println("Name: " + data.getFirst());
        System.out.println("Age: " + data.getSecond());
        System.out.println("Active: " + data.getThird());
    }
}

আউটপুট:

Name: Bob
Age: 35
Active: true

৪. Tuples থেকে উপাদান লুপের মাধ্যমে অ্যাক্সেস (Javatuples)

কোড উদাহরণ:

import org.javatuples.Quintet;

public class TupleIteration {
    public static void main(String[] args) {
        // Quintet তৈরি
        Quintet<String, Integer, String, Double, Boolean> data = 
                new Quintet<>("John", 40, "Manager", 75000.5, true);

        // লুপের মাধ্যমে অ্যাক্সেস
        for (Object value : data) {
            System.out.println(value);
        }
    }
}

আউটপুট:

John
40
Manager
75000.5
true

৫. Tuple থেকে List এ রূপান্তর এবং অ্যাক্সেস

কোড উদাহরণ:

import org.javatuples.Quartet;
import java.util.List;

public class TupleToListAccess {
    public static void main(String[] args) {
        // Quartet তৈরি
        Quartet<String, Integer, Double, Boolean> data = 
                new Quartet<>("Alice", 22, 3.75, true);

        // Tuple থেকে List
        List<Object> dataList = data.toList();

        // List থেকে উপাদান অ্যাক্সেস
        System.out.println("Name: " + dataList.get(0));
        System.out.println("Age: " + dataList.get(1));
        System.out.println("GPA: " + dataList.get(2));
        System.out.println("Graduated: " + dataList.get(3));
    }
}

আউটপুট:

Name: Alice
Age: 22
GPA: 3.75
Graduated: true

  • Javatuples লাইব্রেরি: getValue(index) এবং নির্দিষ্ট মেথড (যেমন getValue0, getValue1) ব্যবহার করে উপাদান অ্যাক্সেস করতে।
  • Vavr লাইব্রেরি: _1, _2, _3 ব্যবহার করে সহজে ডেটা পড়া।
  • Custom Tuples: কাস্টম ক্লাস তৈরি করে সহজে ডেটা ম্যানেজ করা যায়।
  • List Conversion: Tuple থেকে List এ রূপান্তর করে উপাদান অ্যাক্সেস করা যায়।

এই কৌশলগুলো ব্যবহার করে Tuples থেকে ডেটা অ্যাক্সেস দ্রুত এবং কার্যকরভাবে করা সম্ভব।

Content added By

জাভাতে Tuples একটি ডেটা স্ট্রাকচার যা একাধিক ভ্যালু ধারণ করতে পারে। Javatuples লাইব্রেরি ব্যবহার করে Tuples-এ থাকা বিভিন্ন মান অ্যাক্সেস করার জন্য Accessor Methods সরবরাহ করা হয়েছে, যেমন getValue0(), getValue1(), ইত্যাদি।


Accessor Methods (getValue0(), getValue1(), ...)

1. Tuples এবং Accessor Methods

  • Tuples-এর ভ্যালু অ্যাক্সেস করার জন্য getValueX() মেথড ব্যবহার করা হয়।
    • getValue0(): প্রথম ভ্যালু।
    • getValue1(): দ্বিতীয় ভ্যালু।
    • getValue2(): তৃতীয় ভ্যালু।

2. Example: Triplet Access

import org.javatuples.Triplet;

public class TupleAccessExample {
    public static void main(String[] args) {
        // Create a Triplet Tuple
        Triplet<String, Integer, Double> triplet = new Triplet<>("Alice", 25, 85.5);

        // Access elements using getValueX()
        String name = triplet.getValue0(); // First element
        int age = triplet.getValue1();    // Second element
        double score = triplet.getValue2(); // Third element

        // Print the accessed values
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Score: " + score);
    }
}

আউটপুট:

Name: Alice
Age: 25
Score: 85.5

Example: Access Elements from Quartet, Quintet, and Sextet

1. Quartet (4 Elements)

import org.javatuples.Quartet;

public class QuartetAccessExample {
    public static void main(String[] args) {
        Quartet<String, Integer, String, Boolean> data = new Quartet<>("John", 30, "Developer", true);

        // Access elements
        String name = data.getValue0();
        int age = data.getValue1();
        String role = data.getValue2();
        boolean active = data.getValue3();

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Role: " + role);
        System.out.println("Active: " + active);
    }
}

আউটপুট:

Name: John
Age: 30
Role: Developer
Active: true

2. Quintet (5 Elements)

import org.javatuples.Quintet;

public class QuintetAccessExample {
    public static void main(String[] args) {
        Quintet<String, String, Integer, Boolean, Double> product = 
                new Quintet<>("Laptop", "Electronics", 5, true, 1200.99);

        // Access elements
        String name = product.getValue0();
        String category = product.getValue1();
        int quantity = product.getValue2();
        boolean inStock = product.getValue3();
        double price = product.getValue4();

        System.out.println("Product: " + name);
        System.out.println("Category: " + category);
        System.out.println("Quantity: " + quantity);
        System.out.println("In Stock: " + inStock);
        System.out.println("Price: $" + price);
    }
}

আউটপুট:

Product: Laptop
Category: Electronics
Quantity: 5
In Stock: true
Price: $1200.99

3. Sextet (6 Elements)

import org.javatuples.Sextet;

public class SextetAccessExample {
    public static void main(String[] args) {
        Sextet<String, String, Integer, Double, Boolean, String> employee = 
                new Sextet<>("Alice", "Manager", 10, 75000.0, true, "New York");

        // Access elements
        String name = employee.getValue0();
        String position = employee.getValue1();
        int experience = employee.getValue2();
        double salary = employee.getValue3();
        boolean active = employee.getValue4();
        String location = employee.getValue5();

        System.out.println("Name: " + name);
        System.out.println("Position: " + position);
        System.out.println("Experience: " + experience);
        System.out.println("Salary: $" + salary);
        System.out.println("Active: " + active);
        System.out.println("Location: " + location);
    }
}

আউটপুট:

Name: Alice
Position: Manager
Experience: 10
Salary: $75000.0
Active: true
Location: New York

Generic Access (toList())

toList() মেথড ব্যবহার করে Tuples কে লিস্টে রূপান্তর করা যায় এবং ইন্ডেক্স ব্যবহার করে মান অ্যাক্সেস করা যায়।

Example:

import org.javatuples.Triplet;
import java.util.List;

public class TupleToListExample {
    public static void main(String[] args) {
        Triplet<String, Integer, Double> data = new Triplet<>("Alice", 25, 85.5);

        // Convert Tuple to List
        List<Object> dataList = data.toList();

        // Access elements using index
        System.out.println("First Element: " + dataList.get(0)); // Alice
        System.out.println("Second Element: " + dataList.get(1)); // 25
        System.out.println("Third Element: " + dataList.get(2)); // 85.5
    }
}

Accessor Methods Summary

Tuple TypeAccessor Methods
PairgetValue0(), getValue1()
TripletgetValue0(), getValue1(), getValue2()
QuartetgetValue0(), getValue1(), getValue2(), getValue3()
QuintetgetValue0(), getValue1(), getValue2(), getValue3(), getValue4()
SextetgetValue0(), getValue1(), getValue2(), getValue3(), getValue4(), getValue5()

  • Accessor Methods (getValueX()) Tuples থেকে নির্দিষ্ট ভ্যালু সহজেই অ্যাক্সেস করতে সাহায্য করে।
  • Tuples কে List এ রূপান্তর করার মাধ্যমে ইন্ডেক্স ব্যবহার করে মান অ্যাক্সেস করা যায়।
  • Tuples এর ইমিউটেবল প্রকৃতি ডেটা ম্যানেজমেন্টকে থ্রেড-সেফ এবং নির্ভরযোগ্য করে তোলে।

এই পদ্ধতি ব্যবহার করে জাভাতে Tuples-এ থাকা একাধিক মান দক্ষতার সাথে অ্যাক্সেস এবং পরিচালনা করা যায়।

Content added By
Promotion

Are you sure to start over?

Loading...