Guava-booleans

提供:Dev Guides
移動先:案内検索

グアバ-ブールクラス

ブールは、プリミティブ型のブールのユーティリティクラスです。

クラス宣言

以下は com.google.common.primitives.Booleans クラスの宣言です-

@GwtCompatible(emulated = true)
   public final class Booleans
      extends Object

方法

Sr.No Method & Description
1

static List<Boolean> asList(boolean…​ backingArray)

Arrays.asList(Object [])と同様に、指定された配列に連動する固定サイズのリストを返します。

2

static int compare(boolean a, boolean b)

指定された2つのブール値を標準的な方法で比較します(falseはtrueより小さいと見なされます)。

3

static boolean[] concat(boolean[]…​ arrays)

提供された各配列から単一の配列に結合された値を返します。

4

static boolean contains(boolean[] array, boolean target)

ターゲットが配列内のどこかの要素として存在する場合、trueを返します。

5

static int countTrue(boolean…​ values)

真の値の数を返します。

6

static boolean[] ensureCapacity(boolean[] array, int minLength, int padding)

配列と同じ値を含む配列を返しますが、指定された最小長であることが保証されています。

7

static int hashCode(boolean value)

値のハッシュコードを返します。呼び出しの結果と等しい((ブール)値).hashCode()。

8

static int indexOf(boolean[] array, boolean target)

配列内の値ターゲットの最初の外観のインデックスを返します。

9

static int indexOf(boolean[] array, boolean[] target)

配列内の指定されたターゲットの最初のオカレンスの開始位置を返します。そのようなオカレンスがない場合は-1を返します。

10

static String join(String separator, boolean…​ array)

指定されたブール値を区切り文字で区切った文字列を返します。

11

static int lastIndexOf(boolean[] array, boolean target)

配列内の値ターゲットの最後の外観のインデックスを返します。

12

static Comparator<boolean[]> lexicographicalComparator()

2つのブール配列を辞書式に比較するコンパレーターを返します。

13

static boolean[] toArray(Collection<Boolean> collection)

ブールインスタンスのコレクションをプリミティブなブール値の新しい配列にコピーします。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • java.lang.Object

ブール値クラスの例

たとえば、 C:/> Guava で選択したエディターを使用して、次のJavaプログラムを作成します。

GuavaTester.java

import java.util.List;
import com.google.common.primitives.Booleans;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testBooleans();
   }

   private void testBooleans() {
      boolean[] booleanArray = {true,true,false,true,true,false,false};

     //convert array of primitives to array of objects
      List<Boolean> objectArray = Booleans.asList(booleanArray);
      System.out.println(objectArray.toString());

     //convert array of objects to array of primitives
      booleanArray = Booleans.toArray(objectArray);
      System.out.print("[ ");

      for(int i = 0; i< booleanArray.length ; i++) {
         System.out.print(booleanArray[i] + " ");
      }

      System.out.println("]");

     //check if element is present in the list of primitives or not
      System.out.println("true is in list? " + Booleans.contains(booleanArray, true));

     //return the first index of element
      System.out.println("true position in list " + Booleans.indexOf(booleanArray, true));

     //Returns the count of true values
      System.out.println("true occured: " + Booleans.countTrue());

     //Returns the comparisons
      System.out.println("false Vs true: " + Booleans.compare(false, true));
      System.out.println("false Vs false: " + Booleans.compare(false, false));
      System.out.println("true Vs false: " + Booleans.compare(true, false));
      System.out.println("true Vs true: " + Booleans.compare(true, true));
   }
}

結果を確認する

次のように javac コンパイラを使用してクラスをコンパイルします-

C:\Guava>javac GuavaTester.java

GuavaTesterを実行して結果を確認します。

C:\Guava>java GuavaTester

結果をご覧ください。

[true, true, false, true, true, false, false]
[ true true false true true false false ]
true is in list? true
true position in list 0
true occured: 0
false Vs true: -1
false Vs false: 0
true Vs false: 1
true Vs true: 0