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

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

ConcurrentLinkedDeque में getLast() - टेल एलिमेंट एक्सेस

getLast() डीक के अंतिम (टेल) एलिमेंट को रिटर्न करता है बिना हटाए। खाली डीक पर 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 - getLast() डेमो

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 last = concurrentLinkedDeque.getLast();
                actionEvent.setText("अंतिम: " + last);
            } catch (Exception e) {
                actionEvent.setText("डीक खाली है");
            }
        });
    }
}

एक्सेस मेथड्स तुलना:

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

// सुरक्षित - null चेक
String last = deque.peekLast();
if (last != null) { /* उपयोग करें */ }

// या try-catch
try {
    String last = deque.getLast();
} catch (NoSuchElementException e) {
    // खाली डीक
}

आउटपुट

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

क्लिक के बाद: अंतिम: प्रसाद

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

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

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

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

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

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

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

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

© कॉपीराइट https://in.wsxdn.com सर्वाधिकार सुरक्षित
मेथडपोजीशनखाली परहटाता है?
getFirst()हेडExceptionनहीं
peekFirst()हेडnullनहीं
getLast()टेलExceptionनहीं
peekLast()टेलnullनहीं
removeFirst()हेडExceptionहां
pollFirst()हेडnullहां
removeLast()टेलExceptionहां
pollLast()टेलnullहां