分享

BooleanUtils就这1张图,必备(四)

 WindySky 2017-08-01

开心一笑

【一女走夜路,突然看到一男张开双臂向她走来,
做拥抱状,上前就是一脚.男子倒地大哭,
说:都第三块了,我招谁惹谁了,带块玻璃回家就这么难么?】

视频教程

大家好,我录制的视频《Java之优雅编程之道》已经在CSDN学院发布了,有兴趣的同学可以购买观看,相信大家一定会收获到很多知识的。谢谢大家的支持……

视频地址:http://edu.csdn.net/lecturer/994

提出问题

Lang3中BooleanUtils如何使用???

解决问题

美图

BooleanUtils用来操作基础布尔或者布尔对象,很多方法在工作中可以经常使用,下面是该工具类的一个蓝图:

BooleanUtils

异或逻辑方法

boolean xor(boolean… array)

Boolean xor(Boolean… array)

   //和自己相同的数值异或运算为0(false),不同的为1(true)
   BooleanUtils.xor(true, true)   = false
   BooleanUtils.xor(false, false) = false
   BooleanUtils.xor(true, false)  = true
   BooleanUtils.xor(true, false)  = true

且逻辑方法

and(boolean… array)

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

    BooleanUtils.and(Boolean.TRUE, Boolean.TRUE)      = Boolean.TRUE
   BooleanUtils.and(Boolean.FALSE, Boolean.FALSE)    = Boolean.FALSE
   BooleanUtils.and(Boolean.TRUE, Boolean.FALSE)     = Boolean.FALSE
   BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE)   = Boolean.TRUE
   BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE
   BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE)  = Boolean.FALSE

或逻辑方法

or(boolean… array)

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

非方法

Boolean negate(Boolean bool)

   //true 变 false,false 变 true
   BooleanUtils.negate(Boolean.TRUE)  = Boolean.FALSE;
   BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
   BooleanUtils.negate(null)          = null;

判断真假

boolean isTrue(Boolean bool)

boolean isNotTrue(Boolean bool)

boolean isFalse(Boolean bool)

boolean isNotFalse(Boolean bool)

   BooleanUtils.isTrue(Boolean.TRUE)  = true
   BooleanUtils.isTrue(Boolean.FALSE) = false
   BooleanUtils.isTrue(null)          = false

   BooleanUtils.isNotTrue(Boolean.TRUE)  = false
   BooleanUtils.isNotTrue(Boolean.FALSE) = true
   BooleanUtils.isNotTrue(null)          = true

基本数据类型与封装类型转换

boolean toBoolean(Boolean bool)

toBooleanDefaultIfNull(Boolean bool,boolean valueIfNull)

toBoolean(int value)

toBooleanObject(int value)

Boolean toBooleanObject(Integer value)

toBoolean(int value,int trueValue,int falseValue)

toBooleanObject(int value,int trueValue,int falseValue,int nullValue)

   //封装类型转换为基础类型
   BooleanUtils.toBoolean(Boolean.TRUE)  = true
   BooleanUtils.toBoolean(Boolean.FALSE) = false
   //如果为null 返回基础类型false
   BooleanUtils.toBoolean(null)          = false
   //如果为空,默认返回true
   BooleanUtils.toBooleanDefaultIfNull(null, true)     = true

   //0为false ,非0为true
   BooleanUtils.toBoolean(0) = false
   BooleanUtils.toBoolean(1) = true
   BooleanUtils.toBoolean(2) = true

   BooleanUtils.toBoolean(Integer.valueOf(0))    = Boolean.FALSE
   BooleanUtils.toBoolean(Integer.valueOf(1))    = Boolean.TRUE
   BooleanUtils.toBoolean(Integer.valueOf(null)) = null        

   //指定1为true,0为false
   BooleanUtils.toBoolean(0, 1, 0) = false
   BooleanUtils.toBoolean(1, 1, 0) = true

   //指定1为true,2为false
   BooleanUtils.toBoolean(2, 1, 2) = false
   BooleanUtils.toBoolean(2, 2, 0) = true

   //指定0为true,2为false,3为null
   BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE
   BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE
   BooleanUtils.toBooleanObject(3, 1, 2, 3) = null

转换为整形

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)等等

   //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

比较方法

compare(boolean x,boolean y)

 BooleanUtils.compare(true,false) = 1
 BooleanUtils.compare(true,true) = 0  

转化为字符串

toString(boolean bool,String trueString,String falseString)

String toStringYesNo(boolean bool)

String toStringOnOff(boolean bool)

String toStringTrueFalse(boolean bool)

String toString(Boolean bool,String trueString,String falseString,String nullString)

   //转化为字符串,true 转化为“true” false 转化为 “false”     
   BooleanUtils.toString(true, "true", "false")   = "true"
   BooleanUtils.toString(false, "true", "false")  = "false"  

   //true 返回 yes  false 返回 no        
   BooleanUtils.toStringYesNo(true)   = "yes"
   BooleanUtils.toStringYesNo(false)  = "no" 

   BooleanUtils.toStringOnOff(true)   = "on"
   BooleanUtils.toStringOnOff(false)  = "off" 

   BooleanUtils.toStringTrueFalse(true)   = "true"
   BooleanUtils.toStringTrueFalse(false)  = "false"   

   //
   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
   BooleanUtils.toString(null, "true", "false", null)           = null;

读书感悟

来自《送你一颗子弹》

  • 如果要给美好的人生一个定义,那就是惬意。如果要给惬意一个定义,那就是三五知己,谈笑风生。

  • 有些人注定是你生命里的癌症,而有些人只是一个喷嚏而已。

  • 幸福其实往往比我们所想象的要简单很多,问题在于如果我们不把所有复杂的不幸都给探索经历一边,不把所有该摔的跤都摔一遍,不把所有的山都给爬一遍,我们就没法相信其实山脚下的那块巴掌大的树荫下就有幸福。

  • 在辽阔的世界面前,一个人有多谦卑,他就会有多快乐。

  • 年少的时候,我觉得孤单是很酷的一件事。长大以后,我觉得孤单是很凄凉的一件事。现在,我觉得孤单不是一件事。

  • 15岁的时候再得到那个5岁的时候热爱的洋娃娃,65岁的时候终于有钱买25岁的时候热爱的那条裙子,又有什么意义呢?

  • 想象中的很多事情都没有发生,而发生的事情却常常是没有想到的。

其他

如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多