Booleans是布爾型基本的實用工具類。
以下是com.google.common.primitives.Booleans類的聲明:
@GwtCompatible(emulated=true) public final class Booleans extends Object
S.N. | 方法及說明 |
---|---|
1 |
static List<Boolean> asList(boolean... backingArray) 返回由指定數(shù)組支持的固定大小的列表,類似 Arrays.asList(Object[]). |
2 |
static int compare(boolean a, boolean b) 比較兩個指定的布爾值的標準方式(假的比真的少考慮以下)。 |
3 |
static boolean[] concat(boolean[]... arrays) 每個數(shù)組提供組合成一個單一的數(shù)組,則值返回。 |
4 |
static boolean contains(boolean[] array, boolean target) 返回true,如果target存在在任何地方數(shù)組元素。 |
5 |
static int countTrue(boolean... values) 返回為true值的數(shù)目。 |
6 |
static boolean[] ensureCapacity(boolean[] array, int minLength, int padding) 返回一個包含相同的值數(shù)組的數(shù)組,但保證是一個規(guī)定的最小長度。 |
7 |
static int hashCode(boolean value) 返回哈希碼的值;等于調(diào)用的結果 ((Boolean) value).hashCode(). |
8 |
static int indexOf(boolean[] array, boolean target) 返回目標數(shù)組的首次出現(xiàn)的索引值。 |
9 |
static int indexOf(boolean[] array, boolean[] target) 返回指定目標的第一個匹配的起始位置數(shù)組內(nèi),或-1,如果不存在。 |
10 |
static String join(String separator, boolean... array) 返回包含由分離器分離所提供的布爾值的字符串。 |
11 |
static int lastIndexOf(boolean[] array, boolean target) 返回target 在數(shù)組中最后一個出現(xiàn)的索引值。 |
12 |
static Comparator<boolean[]> lexicographicalComparator() 返回一個比較器,它比較兩個布爾數(shù)組字典順序。 |
13 |
static boolean[] toArray(Collection<Boolean> collection) 復制Boolean實例集合到原始的布爾值的新數(shù)組。 |
這個類繼承了以下類方法:
java.lang.Object
使用所選擇的任何編輯器創(chuàng)建下面的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上一篇:Guava Table接口下一篇:Guava BigIntegerMath類