Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में ConcurrentLinkedQueue में एलिमेंट कैसे खोजें?

ConcurrentLinkedQueue में contains() मेथड का उपयोग

ConcurrentLinkedQueue एक अनबाउंडेड, थ्रेड-सेफ, FIFO क्यू है जो लिंक्ड नोड्स पर आधारित है। Null एलिमेंट्स की अनुमति नहीं देता।

चरण 1: प्रोजेक्ट बनाएं

Android Studio: File → New Project।

चरण 2: लेआउट (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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/actionEvent"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

चरण 3: MainActivity.java - contains() डेमो

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.appcompat.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("साई");
        concurrentLinkedQueue.add("राम");
        concurrentLinkedQueue.add("कृष्ण");
        concurrentLinkedQueue.add("प्रसाद");
        concurrentLinkedQueue.add("राम"); // डुप्लीकेट

        actionEvent.setText("" + concurrentLinkedQueue);

        actionEvent.setOnClickListener(v -> {
            boolean found = concurrentLinkedQueue.contains("प्रसाद");
            actionEvent.setText("'प्रसाद' मौजूद: " + found);
        });
    }
}

ConcurrentLinkedQueue विशेषताएं:

  • FIFO क्रम: एलिमेंट्स टेल पर जुड़ते हैं, हेड से निकलते हैं
  • थ्रेड-सेफ: CAS (Compare-And-Swap) ऑपरेशन्स, लॉक-फ्री
  • नॉन-ब्लॉकिंग: प्रोड्यूसर/कंज्यूमर कभी ब्लॉक नहीं होते
  • No null: null जोड़ने पर NullPointerException
  • contains() O(n) लीनियर सर्च है

मुख्य मेथड्स:

मेथडविवरण
add(e) / offer(e)टेल पर जोड़ें
poll()हेड से हटाकर रिटर्न (null अगर खाली)
peek()हेड देखें बिना हटाए
contains(o)एलिमेंट मौजूद है? (O(n))
size()एलिमेंट्स काउंट (O(n) - ट्रैवर्सल)

आउटपुट

प्रारंभ: [साई, राम, कृष्ण, प्रसाद, राम]

क्लिक के बाद: 'प्रसाद' मौजूद: true

Android में ConcurrentLinkedQueue में एलिमेंट कैसे खोजें?

Android में ConcurrentLinkedQueue में एलिमेंट कैसे खोजें?

नोट: बार-बार contains() या size() न करें - दोनों O(n) हैं।

  1. Android में ConcurrentLinkedQueue में remove() का उपयोग कैसे करें? Android में ConcurrentLinkedQueue में remove() का उपयोग कैसे करें?

    ConcurrentLinkedQueue क्या है? ConcurrentLinkedQueue एक अनबाउंडेड क्यू है जो लिंक्ड नोड्स पर आधारित है। मल्टीपल थ्रेड्स इसके एलिमेंट्स को थ्रेड-सेफ्टी के साथ एक्सेस कर सकते हैं। एलिमेंट्स FIFO स्ट्रेटेजी के अनुसार ट्रैवल करते हैं और टेल से इन्सर्ट होते हैं। यह null वैल्यूज को अनुमति नहीं देता। उद्

  1. Android में ConcurrentLinkedQueue में size() का उपयोग कैसे करें? Android में ConcurrentLinkedQueue में size() का उपयोग कैसे करें?

    ConcurrentLinkedQueue क्या है? ConcurrentLinkedQueue एक अनबाउंडेड क्यू है जो लिंक्ड नोड्स पर आधारित है। मल्टीपल थ्रेड्स इसके एलिमेंट्स को थ्रेड-सेफ्टी के साथ एक्सेस कर सकते हैं। एलिमेंट्स FIFO स्ट्रेटेजी के अनुसार ट्रैवल करते हैं और टेल से इन्सर्ट होते हैं। यह null वैल्यूज को अनुमति नहीं देता। उद्

  1. Android डिवाइस का सीरियल नंबर कैसे प्राप्त करें? Android डिवाइस का सीरियल नंबर कैसे प्राप्त करें?

    Android डिवाइस का सीरियल नंबर और डिवाइस जानकारी प्राप्त करना यह उदाहरण दर्शाता है कि Android डिवाइस का सीरियल नंबर (`Build.SERIAL`) और टेलीफोनी जानकारी कैसे प्राप्त करें, जिसमें रनटाइम परमिशन हैंडलिंग शामिल है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़