AndroidでArrayBlockingQueueのpoll()メソッドを使う方法を解説
ArrayBlockingQueueとは
具体的な例に入る前に、まずArrayBlockingQueueについて簡単に説明します。ArrayBlockingQueueはFIFO(First In First Out:先入れ先出し)方式で動作するキューです。最初に追加された要素が最も長くキュー内に保持され、最後に追加された要素ほど短い期間しか保持されません。
この記事では、AndroidアプリでArrayBlockingQueueのpoll()メソッドを使用して、キューから要素を取り出す方法を解説します。
手順1:新規プロジェクトの作成
Android Studioで新しいプロジェクトを作成します。メニューから[File]⇒[New Project]を選択し、必要な項目を入力してプロジェクトを作成してください。
手順2:レイアウトファイルの作成
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>
上記のコードでは、ArrayBlockingQueueの要素を表示するためのTextViewを1つ配置しています。
手順3:MainActivity.javaの実装
src/MainActivity.javaに以下のコードを追加します。
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;
public class MainActivity extends AppCompatActivity {
ArrayBlockingQueue 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("sai");
arrayBlockingQueue.add("ram");
arrayBlockingQueue.add("krishna");
arrayBlockingQueue.add("prasad");
actionEvent.setText("" + arrayBlockingQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
actionEvent.setText("" + arrayBlockingQueue.poll());
}
});
}
}上記のコードでは、容量50のArrayBlockingQueueを作成し、「sai」「ram」「krishna」「prasad」の4つの要素を追加しています。初期状態ではキュー全体がTextViewに表示され、TextViewをタップするとpoll()メソッドが呼び出されてキューの先頭要素が取り出されます。
アプリの実行と動作確認
それでは、アプリを実行してみましょう。ここでは、実際のAndroid端末をパソコンに接続しているものとして説明します。Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルのいずれかを開き、ツールバーの[Run]アイコンをクリックします。実行デバイスとして自分のモバイル端末を選択すると、次のようなデフォルト画面が表示されます。

この状態でTextViewをタップすると、poll()メソッドによってキューの先頭要素が取り出され、以下のように画面に表示されます。

このように、poll()メソッドを使うことで、ArrayBlockingQueueから先頭の要素を簡単に取得・削除できます。なお、キューが空の場合、poll()は例外をスローせずにnullを返すため、安全に要素の取得を行える点も覚えておくとよいでしょう。
-
【Android】スナックバー(Snackbar)の使い方をステップごとに解説
このチュートリアルでは、Androidアプリでスナックバー(Snackbar)を使用する方法を、サンプルコードとともに段階的に解説します。スナックバーは画面下部に短いメッセージを一時的に表示できるUIコンポーネントで、Toastとは異なり「再試行(RETRY)」などのアクションボタンを組み込めるのが大きな特徴です。 手順1:新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 手順2:レイアウトファイル(activity_main.xml)を編集する
-
【Android】NavigationViewの実装方法をステップごとに解説
この記事では、AndroidアプリでNavigationView(ナビゲーションビュー)を使用してドロワーメニューを実装する方法を、ステップごとに詳しく解説します。ハンバーガーアイコンから開閉できるサイドメニューは、多くのアプリで採用されている定番のUIです。ステップ1:新しいプロジェクトを作成するAndroid Studioを開き、File → New Project を選択して、必要な情報を入力し新しいプロジェクトを作成します。テンプレートには「Navigation Drawer Activity」を選ぶと、後の作業がスムーズになります。ステップ2:activity_main.xml にコ