zoukankan      html  css  js  c++  java
  • 判断字符、集合的常用方法

    isNotEmpty将空格也作为参数,isNotBlank则排除空格参数

    StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数Stringnull则不会抛出NullPointerException,而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。

    除了构造器,StringUtils中一共有130多个方法,并且都是static的,所以我们可以这样调用StringUtils.xxx()

    publicstaticboolean isEmpty(String str)

    判断某字符串是否为空,为空的标准是str==nullstr.length()==0

    下面是StringUtils判断是否为空的示例:

    StringUtils.isEmpty(null)=true

    StringUtils.isEmpty("")=true

    StringUtils.isEmpty(" ")=false//注意在StringUtils中空格作非空处理

    StringUtils.isEmpty(" ")=false

    StringUtils.isEmpty("bob")=false

    StringUtils.isEmpty(" bob ")=false

    publicstaticboolean isBlank(String str)

    判断某字符串是否为空或长度为0或由空白符(whitespace)构成

    下面是示例:

    StringUtils.isBlank(null)=true

    StringUtils.isBlank("")=true

    StringUtils.isBlank(" ")=true

    StringUtils.isBlank(" ")=true

    StringUtils.isBlank("/t /n /f /r")=true//对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符

    StringUtils.isBlank("/b")=false//"/b"为单词边界符

    StringUtils.isBlank("bob")=false

    StringUtils.isBlank(" bob ")=false

    CollectionUtils提供很多对集合的操作方法,常用的方法如下

    import org.apache.commons.collections.CollectionUtils;

    import java.util.ArrayList;

    import java.util.List;

    publicclassCollectionUtilsTest{

    publicstaticvoid main(String[] args){

    List<Integer> a =newArrayList<Integer>();

    List<Integer> b =null;

    List<Integer> c =newArrayList<Integer>();

    c.add(5);

    c.add(6);

    //判断集合是否为空

    System.out.println(CollectionUtils.isEmpty(a));//true

    System.out.println(CollectionUtils.isEmpty(b));//true

    System.out.println(CollectionUtils.isEmpty(c));//false

    //判断集合是否不为空

    System.out.println(CollectionUtils.isNotEmpty(a));//false

    System.out.println(CollectionUtils.isNotEmpty(b));//false

    System.out.println(CollectionUtils.isNotEmpty(c));//true

    //两个集合间的操作

    List<Integer> e =newArrayList<Integer>();

    e.add(2);

    e.add(1);

    List<Integer> f =newArrayList<Integer>();

    f.add(1);

    f.add(2);

    List<Integer> g =newArrayList<Integer>();

    g.add(12);

    //比较两集合值

    System.out.println(CollectionUtils.isEqualCollection(e,f));//true

    System.out.println(CollectionUtils.isEqualCollection(f,g));//false

    List<Integer> h =newArrayList<Integer>();

    h.add(1);

    h.add(2);

    h.add(3);;

    List<Integer> i =newArrayList<Integer>();

    i.add(3);

    i.add(3);

    i.add(4);

    i.add(5);

    //并集

    System.out.println(CollectionUtils.union(i,h));//[1, 2, 3, 3, 4, 5]

    //交集

    System.out.println(CollectionUtils.intersection(i,h));//[3]

    //交集的补集

    System.out.println(CollectionUtils.disjunction(i,h));//[1, 2, 3, 4, 5]

    //e与h的差

    System.out.println(CollectionUtils.subtract(h,i));//[1, 2]

    System.out.println(CollectionUtils.subtract(i,h));//[3, 4, 5]

    }

    }

     
     
     
     





  • 相关阅读:
    linux之间文件共享的方式
    linux访问windows文件的方法
    linux下samba配置
    linux下ftp搭建
    windws下搭建ftp步骤
    索引的二元高度
    Oracle表的分析统计
    oracle———索引
    这些git技能够你用一年了
    使用 Composer 为 ThinkPHP(3.2.3)框架添加和管理组件
  • 原文地址:https://www.cnblogs.com/samwang88/p/416ccb4e11e0f5dfefc02fb670ed561d.html
Copyright © 2011-2022 走看看