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

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

Giới thiệu về removeFirstOccurrence()

removeFirstOccurrence(Object o) xóa lần xuất hiện đầu tiên của phần tử chỉ định, bắt đầu từ đầu (head) của deque. Trả về true nếu xóa thành công, false nếu không tìm thấy.

ConcurrentLinkedDeque là deque unbounded, thread-safe, lock-free - phù hợp cho môi trường đa luồng cao.

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");
        concurrentLinkedDeque.add("ram"); // "ram" xuất hiện lần 2

        actionEvent.setText("" + concurrentLinkedDeque);

        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Xóa lần xuất hiện ĐẦU TIÊN của "ram" (từ đầu)
                boolean removed = concurrentLinkedDeque.removeFirstOccurrence("ram");
                actionEvent.setText("Đã xóa: " + removed + "\n" + concurrentLinkedDeque);
            }
        });
    }
}

Kết quả

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

Sau click: [sai, krishna, prasad, ram] (đã xóa "ram" đầu tiên ở vị trí 1)

removeFirstOccurrence vs removeLastOccurrence

Phương thức Duyệt từ Xóa lần xuất hiện
removeFirstOccurrence(o) Đầu (head) Đầu tiên gặp
removeLastOccurrence(o) Cuối (tail) Cuối cùng gặp

Ví dụ so sánh

Deque: [A, B, C, B, D]

removeFirstOccurrence("B") → [A, C, B, D]  // Xóa B đầu tiên
removeLastOccurrence("B")  → [A, B, C, D]  // Xóa B cuối cùng

Các phương thức xóa trong Deque

Phương thức Hành động Trả về Rỗng/Không tìm thấy
removeFirstOccurrence(o) Xóa đầu tiên từ head boolean false
removeLastOccurrence(o) Xóa cuối cùng từ tail boolean false
remove(o) Tương đương removeFirstOccurrence boolean false
removeFirst() Xóa phần tử head E Ném Exception
removeLast() Xóa phần tử tail E Ném Exception

Đặc điểm ConcurrentLinkedDeque

  • Unbounded: Không giới hạn kích thước
  • Thread-safe: Lock-free (CAS operations)
  • Hiệu năng cao: Phù hợp high-contention
  • Weakly consistent iterator: Không ném ConcurrentModificationException
  • Không cho phép null: Ném NullPointerException

Khi nào dùng removeFirstOccurrence?

  • Cần xóa một phần tử cụ thể theo thứ tự FIFO
  • Xử lý duplicate entries - chỉ xóa cái đầu tiên
  • Môi trường đa luồng không muốn lock