common Lang3 BooleanUtils使用示例
时间: 2023-07-11 09:08:16 浏览: 37
Commons Lang3 的 BooleanUtils 类提供了一些方便的方法来处理 boolean 值和 Boolean 对象。以下是一些示例:
1. 将 Boolean 对象转换为 boolean 值:
```java
Boolean bool = Boolean.TRUE;
boolean b = BooleanUtils.isTrue(bool); // true
```
2. 将 boolean 值转换为 Boolean 对象:
```java
boolean b = true;
Boolean bool = BooleanUtils.toBooleanObject(b); // Boolean.TRUE
```
3. 将字符串转换为 boolean 值:
```java
String str = "true";
boolean b = BooleanUtils.toBoolean(str); // true
```
4. 将字符串转换为 Boolean 对象:
```java
String str = "false";
Boolean bool = BooleanUtils.toBooleanObject(str); // Boolean.FALSE
```
5. 检查数组中是否存在 true 值:
```java
boolean[] arr = {false, true, false};
boolean hasTrue = BooleanUtils.containsTrue(arr); // true
```
6. 检查数组中是否存在 false 值:
```java
boolean[] arr = {true, true, true};
boolean hasFalse = BooleanUtils.containsFalse(arr); // false
```
这些都是 BooleanUtils 类中的一些常用方法,可以更方便地处理 boolean 值和 Boolean 对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)