ConcurrentLinkedQueue-এ remove() মেথড
remove(Object o) কিউ থেকে নির্দিষ্ট উপাদানের প্রথম ঘটনা (শিরোনাম থেকে শুরু করে) সরিয়ে দেয়। সফল হলে true, না পেলে false ফেরত দেয়। এটি FIFO ক্রমে লিনিয়ার সার্চ করে।
উদাহরণ
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
ধাপ ২: লেআউট XML (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
ধাপ ৩: জাভা কোড (src/MainActivity.java)
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ConcurrentLinkedQueue;
public class MainActivity extends AppCompatActivity {
ConcurrentLinkedQueue<String> concurrentLinkedQueue;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
concurrentLinkedQueue.add("sai");
concurrentLinkedQueue.add("ram");
concurrentLinkedQueue.add("krishna");
concurrentLinkedQueue.add("prasad");
concurrentLinkedQueue.add("ram");
actionEvent.setText("" + concurrentLinkedQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// "prasad" সরান
boolean removed = concurrentLinkedQueue.remove("prasad");
actionEvent.setText("Removed: " + removed + " | " + concurrentLinkedQueue);
}
});
}
}
ফলাফল
ক্লিক করার আগে: [sai, ram, krishna, prasad, ram]
ক্লিক করার পর: [sai, ram, krishna, ram] ("prasad" সরানো হয়েছে)
ConcurrentLinkedQueue রিমুভ মেথড তুলনা
| মেথড | কাজ | ফেরত দেয় | খালি/না পাওয়ায় |
|---|---|---|---|
remove() | শিরোনাম সরিয়ে ফেরত দেয় | সরানো উপাদান | NoSuchElementException |
poll() | শিরোনাম সরিয়ে ফেরত দেয় | সরানো উপাদান | null |
remove(Object) | নির্দিষ্ট উপাদান সরায় | boolean | false |
বহু-থ্রেড পরিবেশে ব্যবহার
// নির্দিষ্ট টাস্ক কিউ থেকে বাতিল করুন
public boolean cancelTask(String taskId) {
return taskQueue.remove(taskId);
}
// সব মিলই সরাতে চাইলে
while (queue.remove("obsolete")) {
// লুপ চলবে যতক্ষণ না মিল পাওয়া বন্ধ হয়
}