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

Cách kiểm tra phần tử tồn tại trong ConcurrentLinkedDeque Android

Giới thiệu về ConcurrentLinkedDeque

ConcurrentLinkedDeque là một deque (hàng đợi hai đầu) không giới hạn dựa trên các nút liên kết. Nhiều luồng có thể truy cập an toàn các phần tử deque đồng thời. Lớp này nằm trong gói java.util.concurrent và yêu cầu API level 21 (Android 5.0 Lollipop) trở lên.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project, điền đầy đủ thông tin cần thiết để tạo dự án mới.

Bước 2: Thiết kế giao diện (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>

Chỉ cần một TextView để hiển thị nội dung deque và kết quả kiểm tra.

Bước 3: Viết logic (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);

        // Thêm một số phần tử mẫu
        concurrentLinkedDeque.add("sai");
        concurrentLinkedDeque.add("ram");
        concurrentLinkedDeque.add("krishna");
        concurrentLinkedDeque.add("prasad");

        actionEvent.setText(concurrentLinkedDeque.toString());

        // Nhấn vào TextView để kiểm tra sự tồn tại của "krishna"
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean contains = concurrentLinkedDeque.contains("krishna");
                actionEvent.setText("Chứa 'krishna': " + contains);
            }
        });
    }
}

Chạy ứng dụng

Kết nối thiết bị Android thật (API 21+) với máy tính. Trong Android Studio, mở file activity và nhấn biểu tượng Run.

Cách kiểm tra phần tử tồn tại trong ConcurrentLinkedDeque Android

Nhấn vào TextView, kết quả sẽ hiển thị:

Cách kiểm tra phần tử tồn tại trong ConcurrentLinkedDeque Android

Lưu ý quan trọng:
  • ConcurrentLinkedDeque chỉ khả dụng từ API 21 (Android 5.0)
  • Phương thức contains() duyệt qua toàn bộ deque nên có độ phức tạp O(n)
  • Phù hợp cho môi trường đa luồng, không cần đồng bộ hóa thủ công
  • Không cho phép phần tử null