কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে ConcurrentLinkedDeque-এ removeFirst() কীভাবে ব্যবহার করবেন?

ConcurrentLinkedDeque-এ removeFirst()

removeFirst() ডিকের শিরোনাম থেকে উপাদান সরিয়ে ফেরত দেয়। খালি ডিকতে NoSuchElementException নিক্ষেপ করে। এটি থ্রেড-সেফ।

উদাহরণ

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

ধাপ ২: লেআউট 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.ConcurrentLinkedDeque;

public class MainActivity extends AppCompatActivity {
    ConcurrentLinkedDeque<String> concurrentLinkedDeque;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        concurrentLinkedDeque = new ConcurrentLinkedDeque<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);

        concurrentLinkedDeque.add("sai");
        concurrentLinkedDeque.add("ram");
        concurrentLinkedDeque.add("krishna");
        concurrentLinkedDeque.add("prasad");

        actionEvent.setText("" + concurrentLinkedDeque);

        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    concurrentLinkedDeque.removeFirst();
                } catch (NoSuchElementException e) {
                    e.printStackTrace();
                }
                actionEvent.setText("" + concurrentLinkedDeque);
            }
        });
    }
}

ফলাফল

ক্লিক করার আগে: [sai, ram, krishna, prasad]
ক্লিক করার পর: [ram, krishna, prasad]

ConcurrentLinkedDeque-এ রিমুভ মেথড তালিকা

মেথডকাজখালি থাকলে
removeFirst()প্রথম সরিয়ে ফেরত দেয়NoSuchElementException
removeLast()শেষ সরিয়ে ফেরত দেয়NoSuchElementException
pollFirst()প্রথম সরিয়ে ফেরত দেয়null
pollLast()শেষ সরিয়ে ফেরত দেয়null
remove(Object)প্রথম মিল সরায়false

বহু-থ্রেড পরিবেশে সেরা অনুশীলন

// নন-ব্লকিং, সেফ পদ্ধতি
String item = concurrentLinkedDeque.pollFirst();
if (item != null) {
    // সফলভাবে সরানো হয়েছে
    process(item);
} else {
    // ডিক খালি
}
  1. অ্যান্ড্রয়েডে LinkedBlockingDeque-এ removeFirst() কীভাবে ব্যবহার করবেন?

  2. অ্যান্ড্রয়েডে ConcurrentLinkedDeque-এ removeLastOccurrence() কীভাবে ব্যবহার করবেন?

  3. অ্যান্ড্রয়েডে ConcurrentLinkedDeque-এ removeLast() কীভাবে ব্যবহার করবেন?

  4. Android-এ ConcurrentLinkedDeque-এ size() পদ্ধতি ব্যবহার করার উপায়