Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ remove() ใน PriorityBlockingQueue ของ Android

ภาพรวมของ PriorityBlockingQueue

PriorityBlockingQueue เป็นคิวแบบไม่มีขอบเขต (unbounded) เรียงลำดับตามความสำคัญ (priority order) ใช้จัดการปัญหา OutOfMemoryError

ตัวอย่างการใช้งาน remove()

ตัวอย่างนี้สาธิตเมธอด remove(Object o) ซึ่งลบสมาชิก ตัวเดียว ที่ตรงกับค่าที่ระบุ (ค้นหาและลบตัวแรกที่พบ) คืน true หากลบสำเร็จ

ขั้นตอนที่ 1-2: สร้างโปรเจกต์และ Layout

สร้างโปรเจกต์ใหม่ ใช้ Layout TextView

ขั้นตอนที่ 3: โค้ด 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.PriorityBlockingQueue;

public class MainActivity extends AppCompatActivity {
    PriorityBlockingQueue<String> priorityBlockingQueue;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        priorityBlockingQueue = new PriorityBlockingQueue<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);
        
        priorityBlockingQueue.add("sai");
        priorityBlockingQueue.add("ram");
        priorityBlockingQueue.add("krishna");
        priorityBlockingQueue.add("prasad");
        priorityBlockingQueue.add("ram");  // ซ้ำ 2 ตัว
        
        actionEvent.setText("" + priorityBlockingQueue);
        
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ลบ "prasad" ตัวเดียว
                boolean removed = priorityBlockingQueue.remove("prasad");
                actionEvent.setText("Removed: " + removed + " | " + priorityBlockingQueue);
            }
        });
    }
}

พฤติกรรมของ remove() ใน PriorityBlockingQueue

  • ค้นหาและลบ ตัวแรกที่ตรงกัน (ตามลำดับ Iterator ซึ่งเป็น Priority order)
  • คืน true หากพบและลบสำเร็จ, false หากไม่พบ
  • Thread-safe - ปลอดภัยสำหรับหลายเธรด
  • ไม่ใช่การลบทุกตัวที่ตรงกัน (ใช้ removeAll() แทน)

ตัวอย่างข้อมูล (Priority Order = Alphabetical สำหรับ String)

เพิ่ม: "sai", "ram", "krishna", "prasad", "ram"
Priority Order (A-Z): [krishna, prasad, ram, ram, sai]
                        ↑          ↑
                   ก่อนหน้า      หลังหน้า

remove("prasad"): ลบ "prasad" ตัวเดียว
ผลลัพธ์: [krishna, ram, ram, sai]

remove() vs removeAll() vs poll()

เมธอดคำอธิบายคืนค่า
remove(Object)ลบตัวเดียวที่ตรงกับ Objectboolean
removeAll(Collection)ลบทุกตัวที่อยู่ใน Collectionboolean
poll()นำออกที่หัว (Priority สูงสุด) คืนค่าE หรือ null
take()นำออกที่หัว Block จนกว่าจะมีข้อมูลE

⚠️ ข้อควรระวัง

  • remove(Object) ต้องใช้ equals() ในการเปรียบเทียบ สำหรับ Custom Object ต้อง Override equals() และ hashCode()
  • PriorityBlockingQueue เรียงตาม Priority ไม่ใช่ Insertion Order
  • Iterator ของ PriorityBlockingQueue ไม่รับประกันลำดับ Priority (แต่ remove() จะค้นหาทุกตัว)

วิธีทดสอบ

  1. รันแอป ดูคิว (เรียง A-Z): [krishna, prasad, ram, ram, sai]
  2. คลิก TextView → เรียก remove("prasad")
  3. ผลลัพธ์: [krishna, ram, ram, sai] - "prasad" หายไป
  4. คลิกอีกครั้ง remove("prasad") → คืน false (ไม่พบ)

กรณีใช้งานจริง

  • ยกเลิก Task เฉพาะรายการใน Priority Queue
  • ลบรายการที่หมดอายุ (Expired) ออกจากคิว
  • Job scheduling - Cancel specific job