StringUtils和CollectionUtils开发中应用

StringUtils

1.引入依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>

2.常用方法(主要用在if()判断中的常用方法

//引入的包
import org.apache.commons.lang3.StringUtils;
  • public static boolean isEmpty(String str)
    判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true 
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
  • public static boolean isNotEmpty(String str)
    判断某字符串是否非空,等于 !isEmpty(String str)
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("         ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
  • public static boolean 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
  • public static boolean isNotBlank(String str)
    判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成,等于 !isBlank(String str)
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("         ") = false
StringUtils.isNotBlank("\t \n \f \r") = false
StringUtils.isNotBlank("\b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true

CollectionUtils

//引入的包
import org.apache.commons.collections.CollectionUtils;

1 引入依赖

       <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>

2 常用方法

  • 判断集合是否为空
  CollectionUtils.isEmpty(null): true
 CollectionUtils.isEmpty(new ArrayList()): true  
 CollectionUtils.isEmpty({
     a,b}): false
  • 判断集合是否不为空
    CollectionUtils.isNotEmpty(null): false
    CollectionUtils.isNotEmpty(new ArrayList()): false
    CollectionUtils.isNotEmpty({
     a,b}): true
  • 并集
public void testUnion(){
     
    String[] arrayA = new String[] {
      "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
      "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取并集 
    System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
    //[A, B, C, D, E, F, G, H, K]
}
  • 交集
    String[] arrayA = new String[] {
      "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
      "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 
    System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
    //[B, D, F]
  • 交集的补集(析取)
public void testDisjunction(){
     
    String[] arrayA = new String[] {
      "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
      "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 的补集
    System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
    //[A, C, E, G, H, K]
}
  • 差集(扣除)
public void testSubtract(){
     
    String[] arrayA = new String[] {
      "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] {
      "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //arrayA扣除arrayB
    System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB)));
    //[A, C, E]
 
}
  • 集合是否相等
    class Person{
     }
    class Girl extends Person{
     
    }
 
    List<Integer> first = new ArrayList<>();
    List<Integer> second = new ArrayList<>();
    first.add(1);
    first.add(2);
    second.add(2);
    second.add(1);
    Girl goldGirl = new Girl();
    List<Person> boy1 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy1.add(new Girl());
    List<Person> boy2 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy2.add(new Girl());
    //比较两集合值
    System.out.println(CollectionUtils.isEqualCollection(first,second));   //true
    System.out.println(CollectionUtils.isEqualCollection(first,boy1));   //false
    System.out.println(CollectionUtils.isEqualCollection(boy1,boy2));   //false
  • 不可修改的集合
  • 应用场景:c相当于数据库中传来的数据,然后通过 CollectionUtils.unmodifiableCollection()存入s中,现在的c和s值相同,但是s不可以修改,修改就会报错
public void testUnmodifiableCollection(){
     
    Collection<String> c = new ArrayList<>();
    Collection<String> s = CollectionUtils.unmodifiableCollection(c);
    c.add("boy");
    c.add("love");
    c.add("girl");
    //! s.add("have a error");  //修改后报错java.lang.UnsupportedOperationException
    System.out.println(s);
}

你可能感兴趣的:(代码模板)