BooleanUtils 介绍
一、官方API地址
官网地址:BooleanUtils.html
二、BooleanUtils 常用API
1.导入POM文件
这里使用的是:3.9 的版本,还是比较新的
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
2.常用API介绍
BooleanUtils用来操作基础布尔或者布尔对象,很多方法在工作中可以经常使用
3.API的使用
and(boolean… array) 逻辑与
BooleanUtils.and(true, true) = true
BooleanUtils.and(false, false) = false
BooleanUtils.and(true, false) = false
BooleanUtils.and(true, true, false) = false
BooleanUtils.and(true, true, true) = true
compare(boolean x, boolean y)
比较两个布尔值并返回int类型 如果x == y返回0, !x && y 返回小于 0 ,x && !y 返回大于0
BooleanUtils.compare(true,false) = 1
BooleanUtils.compare(true,true) = 0
isFalse(Boolean bool) 是否是假并返回boolean
isTrue(Boolean bool) 是否是真并返回boolean
negate(Boolean bool) 逻辑非
BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE;
BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
BooleanUtils.negate(null) = null;
isNotTrue(Boolean bool)
isNotFalse(Boolean bool)
BooleanUtils.isNotTrue(Boolean.TRUE) = false
BooleanUtils.isNotTrue(Boolean.FALSE) = true
BooleanUtils.isNotTrue(null) = true
or(boolean… array) 逻辑或
BooleanUtils.or(true, true) = true
BooleanUtils.or(false, false) = false
BooleanUtils.or(true, false) = true
BooleanUtils.or(true, true, false) = true
BooleanUtils.or(true, true, true) = true
BooleanUtils.or(false, false, false) = false
toBoolean(Boolean bool) 将对象类型转换为基本数据类型并返回
BooleanUtils.toBoolean(Boolean.TRUE) = true
BooleanUtils.toBoolean(Boolean.FALSE) = false
BooleanUtils.toBoolean(null) = false
toBoolean(int value) 将int类型转换为boolean类型并返回
BooleanUtils.toBoolean(0) = false
BooleanUtils.toBoolean(1) = true
BooleanUtils.toBoolean(2) = true
toBoolean(String str) 将string类型转换为boolean类型并返回
BooleanUtils.toBoolean(null) = false
BooleanUtils.toBoolean("true") = true
BooleanUtils.toBoolean("TRUE") = true
BooleanUtils.toBoolean("tRUe") = true
BooleanUtils.toBoolean("on") = true
BooleanUtils.toBoolean("yes") = true
BooleanUtils.toBoolean("false") = false
BooleanUtils.toBoolean("x gti") = false
BooleanUtils.toBooleanObject("y") = true
BooleanUtils.toBooleanObject("n") = false
BooleanUtils.toBooleanObject("t") = true
BooleanUtils.toBooleanObject("f") = false
toBooleanDefaultIfNull
//如果为空,默认返回true
BooleanUtils.toBooleanDefaultIfNull(null, true) = true
int toInteger(boolean bool) 转换为整形
Integer toIntegerObject(boolean bool) 转换为整形
int toInteger(boolean bool,int trueValue,int falseValue) 转换为整形
int toInteger(Boolean bool,int trueValue,int falseValue,int nullValue) 转换为整形
toInteger(boolean bool) 将boolean类型数据转换为int类型并返回
BooleanUtils.toInteger(true) = 1
BooleanUtils.toInteger(false) = 0
//false是0,true是1
BooleanUtils.toInteger(true) = 1
BooleanUtils.toInteger(false) = 0
//true 转化为 1,false转化为 0
BooleanUtils.toInteger(true, 1, 0) = 1
BooleanUtils.toInteger(false, 1, 0) = 0
//true为1,false为0,null为2
BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1
BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
BooleanUtils.toInteger(null, 1, 0, 2) = 2
toStringOnOff(boolean bool) 将boolean类型数据转换为String类型’on’ or 'off’并返回
BooleanUtils.toStringOnOff(true) = "on"
BooleanUtils.toStringOnOff(false) = "off"
toStringTrueFalse(Boolean bool) 将boolean类型数据转换为String类型’‘true’ or 'false’并返回
BooleanUtils.toStringTrueFalse(true) = "true"
BooleanUtils.toStringTrueFalse(false) = "false"
toStringYesNo(boolean bool) 将boolean类型数据转换为String类型’yes’ or 'no’并返回
BooleanUtils.toStringYesNo(true) = "yes"
BooleanUtils.toStringYesNo(false) = "no"
xor(boolean… array) 异或
BooleanUtils.xor(true, true) = false
BooleanUtils.xor(false, false) = false
BooleanUtils.xor(true, false) = true