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

Android में ConcurrentLinkedDeque से पहला एलिमेंट कैसे प्राप्त करें?

ConcurrentLinkedDeque में getFirst() मेथड का उपयोग

ConcurrentLinkedDeque एक अनबाउंडेड, थ्रेड-सेफ डीक है। getFirst() हेड (पहला) एलिमेंट रिटर्न करता है बिना हटाए। खाली होने पर NoSuchElementException फेंकता है।

चरण 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 - getFirst() डेमो

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.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);

        // प्रारंभिक डेटा
        concurrentLinkedDeque.add("साई");
        concurrentLinkedDeque.add("राम");
        concurrentLinkedDeque.add("कृष्ण");
        concurrentLinkedDeque.add("प्रसाद");

        actionEvent.setText("" + concurrentLinkedDeque);

        // क्लिक पर पहला एलिमेंट प्राप्त करें
        actionEvent.setOnClickListener(v -> {
            try {
                String first = concurrentLinkedDeque.getFirst();
                actionEvent.setText("पहला: " + first);
            } catch (Exception e) {
                actionEvent.setText("डीक खाली है");
            }
        });
    }
}

getFirst() बनाम peekFirst():

मेथडव्यवहारखाली डीक पर
getFirst()पहला एलिमेंट रिटर्नNoSuchElementException
peekFirst()पहला एलिमेंट रिटर्नnull (सुरक्षित)
getLast()अंतिम एलिमेंट रिटर्नNoSuchElementException
peekLast()अंतिम एलिमेंट रिटर्नnull (सुरक्षित)

सुरक्षित पैटर्न:

// सुरक्षित तरीका
String first = concurrentLinkedDeque.peekFirst();
if (first != null) {
    // उपयोग करें
}

आउटपुट

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

क्लिक के बाद: पहला: साई

Android में ConcurrentLinkedDeque से पहला एलिमेंट कैसे प्राप्त करें?

Android में ConcurrentLinkedDeque से पहला एलिमेंट कैसे प्राप्त करें?

नोट: ConcurrentLinkedDeque नॉन-ब्लॉकिंग है - हाई कॉनकरेंसी के लिए बेहतर।

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

    ConcurrentLinkedDeque क्या है? ConcurrentLinkedDeque एक अनबाउंडेड थ्रेड-सेफ डीक्यू है जो लिंक्ड नोड्स पर आधारित है। एकाधिक थ्रेड्स सुरक्षित रूप से डीक्यू के तत्वों तक पहुंच सकते हैं। उदाहरण: pollFirst() मेथड का उपयोग यह उदाहरण दर्शाता है कि Android में ConcurrentLinkedDeque के pollFirst() मेथड का

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

    ConcurrentLinkedDeque क्या है? ConcurrentLinkedDeque एक अनबाउंडेड थ्रेड-सेफ डीक्यू है जो लिंक्ड नोड्स पर आधारित है। एकाधिक थ्रेड्स सुरक्षित रूप से डीक्यू के तत्वों तक पहुंच सकते हैं। उदाहरण: poll() मेथड का उपयोग यह उदाहरण दर्शाता है कि Android में ConcurrentLinkedDeque के poll() मेथड का उपयोग कैसे

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

    ConcurrentLinkedDeque क्या है? ConcurrentLinkedDeque एक अनबाउंडेड डीक्यू है जो लिंक्ड नोड्स पर आधारित है। मल्टीपल थ्रेड्स इसके एलिमेंट्स को थ्रेड-सेफ्टी के साथ एक्सेस कर सकते हैं। उद्देश्य यह उदाहरण दर्शाता है कि Android में ConcurrentLinkedDeque में peekLast() मेथड का उपयोग कैसे करें। चरण 1: नया