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

วิธีค้นหาข้อมูลใน ConcurrentLinkedQueue บน Android

บทนำ

ConcurrentLinkedQueue เป็น Queue แบบ Unbounded, Thread-safe, ใช้ Linked Nodes ทำงานแบบ FIFO เพิ่มที่ Tail, ดึงที่ Head ไม่อนุญาต null values ตัวอย่างนี้สาธิตการใช้ contains() ค้นหาข้อมูล

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: ออกแบบ Layout (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>

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

public class MainActivity extends AppCompatActivity {

    ConcurrentLinkedQueue<String> concurrentLinkedQueue;

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

        concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);

        // เพิ่มข้อมูลตัวอย่าง
        concurrentLinkedQueue.add("sai");
        concurrentLinkedQueue.add("ram");
        concurrentLinkedQueue.add("krishna");
        concurrentLinkedQueue.add("prasad");
        concurrentLinkedQueue.add("ram"); // ค่าซ้ำได้

        actionEvent.setText("" + concurrentLinkedQueue);

        // คลิกเพื่อค้นหา "prasad"
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean found = concurrentLinkedQueue.contains("prasad");
                actionEvent.setText("Found prasad: " + found);
            }
        });
    }
}

การทดสอบแอปพลิเคชัน

รันแอปบนอุปกรณ์จริง

วิธีค้นหาข้อมูลใน ConcurrentLinkedQueue บน Android

คลิก TextView จะแสดง "Found prasad: true"

วิธีค้นหาข้อมูลใน ConcurrentLinkedQueue บน Android

คำอธิบาย contains(Object o)

  • คืน true หากพบองค์ประกอบที่ equals() กับอาร์กิวเมนต์
  • คืน false หากไม่พบ
  • Thread-safe: การค้นหาขณะ Thread อื่น Add/Remove ไม่มีปัญหา
  • Complexity: O(n) - ต้องวนลูปหา (Linked List)

ข้อสังเกต

  • ConcurrentLinkedQueue ไม่มี get(index) เพราะเป็น Linked List (ไม่รองรับ Random Access)
  • ใช้ Iterator หรือ for-each loop เพื่อวนลูปอ่านข้อมูลทั้งหมด
  • เหมาะสำหรับ: Producer-Consumer แบบ Unbounded, High Concurrency

สรุป

ใช้ contains() ค้นหาข้อมูลใน ConcurrentLinkedQueue แบบ Thread-safe คืนค่า Boolean เหมาะสำหรับตรวจสอบก่อนประมวลผล