Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách sử dụng pop() trong ConcurrentLinkedDeque trên Android

Giới thiệu về pop() trong ConcurrentLinkedDeque

pop() lấy và xóa phần tử đầu tiên của deque (tương đương removeFirst()). Đây là hoạt động đặc trưng của Stack (LIFO - Last In First Out).

Khác với poll() trả về null khi rỗng, pop() ném NoSuchElementException nếu deque rỗng.

Ví dụ minh họa

Bước 1: Tạo project

Android Studio → File → New Project.

Bước 2: Layout

<?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>

Bước 3: Code (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) {
                // Pop lấy và xóa phần tử ĐẦU
                concurrentLinkedDeque.pop();
                actionEvent.setText("" + concurrentLinkedDeque);
            }
        });
    }
}

Kết quả

Ban đầu: [sai, ram, krishna, prasad]

Click 1: [ram, krishna, prasad] (đã xóa "sai")

Click 2: [krishna, prasad] (đã xóa "ram")

Click 3: [prasad]

Click 4: []

Click 5: Crash app - ném NoSuchElementException

Xử lý an toàn - Kiểm tra trước khi pop

actionEvent.setOnClickListener(v -> {
    if (!concurrentLinkedDeque.isEmpty()) {
        concurrentLinkedDeque.pop();
        actionEvent.setText("" + concurrentLinkedDeque);
    } else {
        actionEvent.setText("Deque rỗng - không thể pop()");
    }
});

Hoặc dùng poll() an toàn hơn

// poll() trả về null thay vì ném exception
String removed = concurrentLinkedDeque.poll();
if (removed != null) {
    actionEvent.setText("Đã xóa: " + removed + "\n" + concurrentLinkedDeque);
} else {
    actionEvent.setText("Deque rỗng");
}

So sánh pop() vs poll() vs removeFirst()

Phương thức Deque rỗng Trả về Sử dụng khi
pop() Ném NoSuchElementException E (phần tử) Chắc chắn không rỗng
poll() Trả về null E hoặc null Không chắc chắn, an toàn
removeFirst() Ném NoSuchElementException E Tương đương pop()
pollFirst() Trả về null E hoặc null Tương đương poll()

Ứng dụng Stack thread-safe

ConcurrentLinkedDeque<String> stack = new ConcurrentLinkedDeque<>();

// Push (thêm vào đầu)
stack.push("Item 1");
stack.push("Item 2");

// Pop (lấy từ đầu) - LIFO
String top = stack.poll(); // An toàn: "Item 2"
String next = stack.poll(); // "Item 1"