Queue ইন্টারফেসটি Java Collections Framework-এর অংশ এবং এটি FIFO (First In First Out) অর্ডারে উপাদানগুলি সংরক্ষণ করে। Queue সাধারণত একটি ডেটা স্ট্রাকচার হিসেবে ব্যবহৃত হয় যেখানে insertions (আইটেম যোগ করা) এক প্রান্তে এবং deletions (আইটেম মুছে ফেলা) অন্য প্রান্তে ঘটে। Queue ইন্টারফেসের মধ্যে অনেক গুরুত্বপূর্ণ মেথড রয়েছে, যেগুলোর মধ্যে offer(), poll(), এবং peek() মেথডগুলি অত্যন্ত গুরুত্বপূর্ণ।
এই মেথডগুলির মধ্যে পার্থক্য এবং ব্যবহার সম্পর্কে বিস্তারিত আলোচনা করা হবে।
1. offer() Method
offer() মেথডটি Queue-এর শেষে একটি উপাদান যোগ করার জন্য ব্যবহৃত হয়। এটি একটি boolean মান রিটার্ন করে:
true: যদি উপাদানটি সফলভাবে Queue-এ যোগ করা হয়।false: যদি উপাদানটি যোগ করা না যায় (যেমন যদি Queue পূর্ণ থাকে এবং এটি সীমানা নির্ধারিত হয়)।
Syntax:
boolean offer(E e);
E: যে উপাদানটি যোগ করা হবে।
Example:
import java.util.LinkedList;
import java.util.Queue;
public class OfferMethodExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the Queue using offer()
queue.offer("Java");
queue.offer("Python");
queue.offer("JavaScript");
// Printing the Queue
System.out.println("Queue: " + queue);
}
}
Output:
Queue: [Java, Python, JavaScript]
ব্যাখ্যা:
offer()মেথডটি Queue-এর শেষে উপাদান যোগ করেছে। এটিLinkedListদ্বারা বাস্তবায়িত Queue-এ কাজ করেছে, যেখানে উপাদানগুলো FIFO (First In First Out) অর্ডারে যোগ হচ্ছে।
2. poll() Method
poll() মেথডটি Queue থেকে প্রথম উপাদান (প্রথমে যোগ করা উপাদান) মুছে ফেলে এবং সেটি রিটার্ন করে। এটি null রিটার্ন করতে পারে যদি Queue খালি থাকে।
Syntax:
E poll();
E: মুছে ফেলা উপাদানটি।
Example:
import java.util.LinkedList;
import java.util.Queue;
public class PollMethodExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the Queue
queue.offer("Java");
queue.offer("Python");
queue.offer("JavaScript");
// Removing elements from the Queue using poll()
String removedElement = queue.poll();
System.out.println("Removed Element: " + removedElement);
// Printing the Queue after removal
System.out.println("Queue after poll: " + queue);
}
}
Output:
Removed Element: Java
Queue after poll: [Python, JavaScript]
ব্যাখ্যা:
poll()মেথডটি প্রথম উপাদান "Java" মুছে ফেলেছে এবং এটি রিটার্ন করেছে। এরপরQueueথেকে বাকি উপাদানগুলো "Python" এবং "JavaScript" অবশিষ্ট রয়েছে।
3. peek() Method
peek() মেথডটি Queue এর প্রথম উপাদানটি read-onlyভাবে ফেরত দেয়, অর্থাৎ এটি শুধুমাত্র প্রথম উপাদানটি দেখে এবং মুছে ফেলে না। যদি Queue খালি থাকে, তবে এটি null রিটার্ন করে।
Syntax:
E peek();
E: প্রথম উপাদানটি, যা মুছে ফেলা হয়নি।
Example:
import java.util.LinkedList;
import java.util.Queue;
public class PeekMethodExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the Queue
queue.offer("Java");
queue.offer("Python");
queue.offer("JavaScript");
// Viewing the first element using peek()
String firstElement = queue.peek();
System.out.println("First Element: " + firstElement);
// Printing the Queue after peek
System.out.println("Queue after peek: " + queue);
}
}
Output:
First Element: Java
Queue after peek: [Java, Python, JavaScript]
ব্যাখ্যা:
peek()মেথডটি প্রথম উপাদান "Java" দেখিয়েছে, কিন্তু এটি Queue থেকে মুছে ফেলেনি। তাই Queue এর অবস্থা অপরিবর্তিত রয়েছে এবং বাকি উপাদানগুলো একই থাকে।
offer(), poll(), এবং peek() এর মধ্যে পার্থক্য:
| Method | Description | Return Value | When to Use |
|---|---|---|---|
offer() | Adds an element to the queue. | true if added, false if not added. | Use when you want to add an element to the queue. |
poll() | Removes and returns the head element of the queue. | The head element or null if the queue is empty. | Use when you want to remove and return the first element. |
peek() | Returns but does not remove the head element of the queue. | The head element or null if the queue is empty. | Use when you want to check the first element without removing it. |
Use Cases:
- offer():
offer()ব্যবহার করুন যখন আপনি Queue তে নতুন উপাদান যোগ করতে চান, এবং এটি যদি পূর্ণ হয়, তবে একটিfalseরিটার্ন হবে, যা সেই অবস্থায় একটি বিশেষ ব্যবস্থা নেয়ার সুযোগ দেয়।
- poll():
poll()ব্যবহার করুন যখন আপনি Queue থেকে প্রথম উপাদানটি মুছে ফেলতে চান এবং যদি এটি খালি হয়, তখনnullরিটার্ন হবে, এটি যাচাই করার সুবিধা প্রদান করে।
- peek():
peek()ব্যবহার করুন যখন আপনি Queue এর প্রথম উপাদানটি দেখতে চান, কিন্তু এটি মুছে ফেলতে চান না, এবং খালি হলেnullরিটার্ন হবে।
offer()মেথডটি একটি উপাদান Queue-এ যোগ করার জন্য ব্যবহৃত হয় এবং এটি boolean রিটার্ন করে।poll()মেথডটি প্রথম উপাদান Queue থেকে মুছে ফেলে এবং এটি রিটার্ন করে। যদি Queue খালি থাকে, তবেnullরিটার্ন করবে।peek()মেথডটি প্রথম উপাদানটি দেখানোর জন্য ব্যবহৃত হয়, তবে এটি মুছে ফেলে না এবং খালি হলেnullরিটার্ন করে।
এগুলি Queue ইন্টারফেসের কিছু গুরুত্বপূর্ণ মেথড, যা FIFO অর্ডারে উপাদান অ্যাক্সেস ও পরিচালনার জন্য ব্যবহৃত হয়।
Read more