布尔值 是用于基本类型Boolean的实用程序类。它提供 静态效用方法 用于修饰或说明布尔基元,在布尔基元或数组中都找不到。
null
宣言:
@GwtCompatible(emulated=true) public final class Booleans extends Object
下面是Guava Booleans类提供的一些方法: 例外情况:
- 保证能力: 非法数据异常 如果minLength或padding为负数。
- 今天: 空指针异常 如果集合或其任何元素为空。
下表显示了Guava Booleans类提供的一些其他方法: 下面给出了一些示例,展示了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<Boolean> 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<Boolean> 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END