- Guava 教程
- Guava - 首页
- Guava - 概述
- Guava - 环境设置
- Guava - Optional 类
- Guava - Preconditions 类
- Guava - Ordering 类
- Guava - Objects 类
- Guava - Range 类
- Guava - Throwables 类
- Guava - 集合工具类
- Guava - 缓存工具类
- Guava - 字符串工具类
- Guava - 原生类型工具类
- Guava - 数学工具类
- Guava 有用资源
- Guava - 快速指南
- Guava - 有用资源
- Guava - 讨论
Guava - 布尔值类
Booleans 是一个用于布尔值基本类型的工具类。
类声明
以下是com.google.common.primitives.Booleans类的声明:
@GwtCompatible(emulated = true)
public final class Booleans
extends Object
方法
| 序号 | 方法及描述 |
|---|---|
| 1 |
static List<Boolean> asList(boolean... backingArray) 返回一个由指定数组支持的固定大小列表,类似于Arrays.asList(Object[])。 |
| 2 |
static int compare(boolean a, boolean b) 以标准方式比较两个指定的布尔值(false被认为小于true)。 |
| 3 |
static boolean[] concat(boolean[]... arrays) 返回来自每个提供的数组组合成单个数组的值。 |
| 4 |
static boolean contains(boolean[] array, boolean target) 如果目标作为元素存在于数组中的任何位置,则返回true。 |
| 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) 返回目标值在数组中最后一次出现时的索引。 |
| 12 |
static Comparator<boolean[]> lexicographicalComparator() 返回一个按字典顺序比较两个布尔数组的比较器。 |
| 13 |
static boolean[] toArray(Collection<Boolean> collection) 将布尔实例的集合复制到一个新的原始布尔值数组中。 |
继承的方法
此类继承自以下类的方法:
- java.lang.Object
Booleans 类的示例
使用您选择的任何编辑器创建以下 Java 程序,例如在C:/> Guava目录下
GuavaTester.java
import 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