push() মেথড কি?
push(E e) ডিকের শিরোনামে (head/first position) উপাদান যোগ করে। এটি addFirst(e)-এর সমতুল্য। স্ট্যাক-এর মতো আচরণের জন্য ব্যবহার করা হয় (LIFO)।
উদাহরণ: ConcurrentLinkedDeque-এ push() ব্যবহার
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
ধাপ ২: লেআউট 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) {
// শিরোনামে উপাদান যোগ করুন (push = addFirst)
concurrentLinkedDeque.push("mammahe");
actionEvent.setText("" + concurrentLinkedDeque);
}
});
}
}
ফলাফল
ক্লিক করার আগে: [sai, ram, krishna, prasad]
ক্লিক করার পর: [mammahe, sai, ram, krishna, prasad]
push() vs pop() vs peek()
| মেথড | কাজ | উপাদান সরায়? |
|---|---|---|
push(e) | শিরোনামে যোগ করে | না |
pop() | শিরোনাম থেকে সরিয়ে ফেরত দেয় | হ্যাঁ |
peek() | শিরোনাম দেখায় | না |
স্ট্যাক বাস্তবায়ন
// স্ট্যাক হিসেবে ব্যবহার
ConcurrentLinkedDeque<String> stack = new ConcurrentLinkedDeque<>();
stack.push("A"); // [A]
stack.push("B"); // [B, A]
stack.push("C"); // [C, B, A]
String top = stack.pop(); // "C" ফেরত দেয়, [B, A] থাকে