জাভাতে 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 Type | Accessor Methods |
|---|---|
| Pair | getValue0(), getValue1() |
| Triplet | getValue0(), getValue1(), getValue2() |
| Quartet | getValue0(), getValue1(), getValue2(), getValue3() |
| Quintet | getValue0(), getValue1(), getValue2(), getValue3(), getValue4() |
| Sextet | getValue0(), getValue1(), getValue2(), getValue3(), getValue4(), getValue5() |
- Accessor Methods (
getValueX()) Tuples থেকে নির্দিষ্ট ভ্যালু সহজেই অ্যাক্সেস করতে সাহায্য করে। - Tuples কে List এ রূপান্তর করার মাধ্যমে ইন্ডেক্স ব্যবহার করে মান অ্যাক্সেস করা যায়।
- Tuples এর ইমিউটেবল প্রকৃতি ডেটা ম্যানেজমেন্টকে থ্রেড-সেফ এবং নির্ভরযোগ্য করে তোলে।
এই পদ্ধতি ব্যবহার করে জাভাতে Tuples-এ থাকা একাধিক মান দক্ষতার সাথে অ্যাক্সেস এবং পরিচালনা করা যায়।
Content added By
Read more