Booleans是布爾型基本的實用工具類。
類聲明
以下是com.google.common.primitives.Booleans類的聲明:
@GwtCompatible(emulated=true) public final class Booleans extends Object
方法
| S.N. | 方法及說明 | 
|---|---|
| 1 | static List<Boolean> asList(boolean... backingArray) 返回由指定數組支持的固定大小的列表,類似 Arrays.asList(Object[]). | 
| 2 | static int compare(boolean a, boolean b) 比較兩個指定的布爾值的標準方式(假的比真的少考慮以下)。 | 
| 3 | static boolean[] concat(boolean[]... arrays) 每個數組提供組合成一個單一的數組,則值返回。 | 
| 4 | static boolean contains(boolean[] array, boolean target) 返回true,如果target存在在任何地方數組元素。 | 
| 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) 返回target 在數組中最後一個出現的索引值。 | 
| 12 | static Comparator<boolean[]> lexicographicalComparator() 返回一個比較器,它比較兩個布爾數組字典順序。 | 
| 13 | static boolean[] toArray(Collection<Boolean> collection) 複製Boolean實例集合到原始的布爾值的新數組。 | 
方法繼承
這個類繼承了以下類方法:
- 
		java.lang.Object 
Booleans 示例
使用所選擇的任何編輯器創建下麵的java程式 C:/> Guava
GuavaTester.javaimport 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 Chars類
												下一篇:
								Guava數學工具
					
					