Java Booleans类
Booleans 是原始类型Boolean的一个实用类。它提供了与布尔基元有关的 静态实用方法 ,这些方法 在布尔或数组中都没有找到。
声明 :
@GwtCompatible(emulated=true)
public final class Booleans
extends Object
以下是Guava Booleans类提供的一些方法:
异常情况
- ensureCapacity : 如果minLength或padding为负数,则出现IllegalArgumentException。
- toArray : 如果集合或其任何元素为空,则出现NullPointerException。
下表显示了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 occurrence of false
System.out.println(Booleans.indexOf(arr, false));
}
}
输出:
1