ArrayBlockingQueue में isEmpty() मेथड का उपयोग
isEmpty() true रिटर्न करता है अगर क्यू में शून्य एलिमेंट्स हैं। यह size() == 0 के समतुल्य है लेकिन अधिक पठनीय।
चरण 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 - isEmpty() डेमो
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;
public class MainActivity extends AppCompatActivity {
ArrayBlockingQueue<String> arrayBlockingQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayBlockingQueue = new ArrayBlockingQueue<>(50);
final TextView actionEvent = findViewById(R.id.actionEvent);
arrayBlockingQueue.add("साई");
arrayBlockingQueue.add("राम");
arrayBlockingQueue.add("कृष्ण");
arrayBlockingQueue.add("प्रसाद");
actionEvent.setText("" + arrayBlockingQueue);
actionEvent.setOnClickListener(v -> {
@SuppressLint("SetTextI18n")
boolean empty = arrayBlockingQueue.isEmpty();
actionEvent.setText("क्या खाली है? " + empty);
});
}
}
खाली चेक मेथड्स तुलना:
| मेथड | रिटर्न | उपयोग |
|---|---|---|
isEmpty() | boolean | पठनीय, मानक कलेक्शन इंटरफेस |
size() == 0 | boolean | समतुल्य, कम क्लीन |
peek() == null | boolean | हेड चेक के साथ संयुक्त |
उपयोग केस:
// कंज्यूमर लूप में
while (!queue.isEmpty()) {
String item = queue.poll();
process(item);
}
// या ब्लॉकिंग कंज्यूमर
String item = queue.take(); // इंतजार करता है जब तक एलिमेंट न हो
आउटपुट
प्रारंभ: [साई, राम, कृष्ण, प्रसाद]
क्लिक के बाद: क्या खाली है? false


नोट: खाली क्यू पर remove() या element() कॉल करने से Exception फेंकता है - पहले isEmpty() चेक करें।