布尔类 |番石榴 |Java
Booleans是基本类型 Boolean 的实用程序类。它提供了与布尔原语有关的静态实用方法,这些方法在布尔或数组中都没有。
宣言 :
@GwtCompatible(emulated=true)
public final class Booleans
extends Object
下面给出的是 Guava Booleans Class 提供的一些方法:
例外:
- ensureCapacity : IllegalArgumentException如果 minLength 或 padding 为负数。
- toArray : NullPointerException如果集合或其任何元素为空。
下表显示了 Guava Booleans Class 提供的一些其他方法:
下面给出了一些示例,显示了 Guava Booleans 类方法的实现:
示例 1:
// Java code to show implementation
// of Guava Booleans.asList() method
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
// Driver method
public static void main(String[] args)
{
boolean arr[] = { true, false, true, false, true };
// Using Booleans.asList() method which
// converts array of primitives to array of objects
List myList = Booleans.asList(arr);
// Displaying the elements
System.out.println(myList);
}
}
输出 :
[true, false, true, false, true]
示例 2:
// Java code to show implementation
// of Guava Booleans.toArray() method
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
// Driver method
public static void main(String[] args)
{
List myList = Arrays.asList(true, false, true, false, true);
// Using Booleans.toArray() method which
// converts a List of Booleans to an
// array of boolean
boolean[] arr = Booleans.toArray(myList);
// Displaying the elements
System.out.println(Arrays.toString(arr));
}
}
输出 :
[true, false, true, false, true]
示例 3:
// Java code to show implementation
// of Guava Booleans.concat() method
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
// Driver method
public static void main(String[] args)
{
boolean[] arr1 = { true, false, true };
boolean[] arr2 = { false, true };
// Using Booleans.concat() method which
// combines arrays from specified
// arrays into a single array
boolean[] arr = Booleans.concat(arr1, arr2);
// Displaying the elements
System.out.println(Arrays.toString(arr));
}
}
输出 :
[true, false, true, false, true]
示例 4:
// Java code to show implementation
// of Guava Booleans.contains() method
import com.google.common.primitives.Booleans;
class GFG {
// Driver method
public static void main(String[] args)
{
boolean[] arr = { true, false, true, false, true };
// Using Booleans.contains() method which
// checks if element is present in array
// or not
System.out.println(Booleans.contains(arr, true));
System.out.println(Booleans.contains(arr, false));
}
}
输出 :
true
true
示例 5:
// Java code to show implementation
// of Guava Booleans.compare() method
import com.google.common.primitives.Booleans;
class GFG {
// Driver method
public static void main(String[] args)
{
// To compare true vs true
System.out.println(Booleans.compare(true, true));
}
}
输出 :
0
示例 6:
// Java code to show implementation
// of Guava Booleans.indexOf() method
import com.google.common.primitives.Booleans;
class GFG {
// Driver method
public static void main(String[] args)
{
boolean[] arr = { true, false, true, false, true };
// To print index of first occurence of false
System.out.println(Booleans.indexOf(arr, false));
}
}
输出 :
1