Guava - 布林值類



Booleans 是一個用於布林值基本型別的工具類。

類宣告

以下是com.google.common.primitives.Booleans類的宣告:

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

方法

序號 方法及描述
1

static List<Boolean> asList(boolean... backingArray)

返回一個由指定陣列支援的固定大小列表,類似於Arrays.asList(Object[])。

2

static int compare(boolean a, boolean b)

以標準方式比較兩個指定的布林值(false被認為小於true)。

3

static boolean[] concat(boolean[]... arrays)

返回來自每個提供的陣列組合成單個數組的值。

4

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

如果目標作為元素存在於陣列中的任何位置,則返回true。

5

static int countTrue(boolean... values)

返回為true的值的數量。

6

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

返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。

7

static int hashCode(boolean value)

返回值的雜湊碼;等於呼叫((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()

返回一個按字典順序比較兩個布林陣列的比較器。

13

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

將布林例項的集合複製到一個新的原始布林值陣列中。

繼承的方法

此類繼承自以下類的方法:

  • java.lang.Object

Booleans 類的示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在C:/> Guava目錄下

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
guava_primitive_utilities.htm
廣告
© . All rights reserved.