LinkedBlockingDeque เป็นคลาสที่ implement จาก Collection interface และ AbstractQueue ใช้ linked nodes รองรับ optional capacity ช่วยลดการสูญเสียหน่วยความจำใน Android
ตัวอย่างโครงการ Android
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio
File → New Project → เลือก Empty Activity → กรอกข้อมูลโปรเจกต์
ขั้นตอนที่ 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:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/actionEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_marginTop="30dp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ด MainActivity (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.LinkedBlockingDeque;
public class MainActivity extends AppCompatActivity {
LinkedBlockingDeque<String> linkedBlockingDeque;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linkedBlockingDeque = new LinkedBlockingDeque<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
// เพิ่มข้อมูล
linkedBlockingDeque.add("sai");
linkedBlockingDeque.add("ram");
linkedBlockingDeque.add("krishna");
linkedBlockingDeque.add("prasad");
linkedBlockingDeque.add("ram"); // ซ้ำ
// แสดง deque เริ่มต้น
actionEvent.setText("" + linkedBlockingDeque);
// กด TextView เพื่อตรวจสอบ contains
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ตรวจสอบว่า "ram" อยู่ใน deque หรือไม่
boolean result = linkedBlockingDeque.contains("ram");
actionEvent.setText("" + result);
}
});
}
}
การทำงานของ contains()
- คืนค่า true หากพบ element ที่ระบุใน deque
- คืนค่า false หากไม่พบ
- ใช้
equals()ในการเปรียบเทียบ - เวลา complexity: O(n) - ต้องสแกนทีละตัว
ผลลัพธ์ที่คาดหวัง
- แอปเริ่มต้น: TextView แสดง
[sai, ram, krishna, prasad, ram] - คลิก TextView: แสดง
true(เพราะ "ram" อยู่ใน deque)
เมธอดอื่นๆ ที่น่าสนใจของ LinkedBlockingDeque
| เมธอด | คำอธิบาย |
|---|---|
add(e) | เพิ่มท้าย (throw exception ถ้าเต็ม) |
offer(e) | เพิ่มท้าย (คืน false ถ้าเต็ม) |
put(e) | เพิ่มท้าย (บล็อกจนกว่าจะมีที่) |
remove() | นำออกจากหัว (throw exception ถ้าว่าง) |
poll() | นำออกจากหัว (คืน null ถ้าว่าง) |
take() | นำออกจากหัว (บล็อกจนกว่าจะมีข้อมูล) |
peek() | ดูหัว deque โดยไม่นำออก |
size() | จำนวน element |
หมายเหตุ
- LinkedBlockingDeque เป็น thread-safe เหมาะสำหรับ producer-consumer pattern
- ใช้
LinkedBlockingDeque<String>()ระบุ generic type เพื่อ type safety - API level 21+ (LOLLIPOP) สำหรับบางเมธอด