依赖:
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
-- ------------------------------万恶的分割线 -------------------------------
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
public class CollectionUtilsTest {
public static void main(String[] args) {
Lista = new ArrayList();
Listb = null;
Listc = new ArrayList();
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
//两个集合间的操作
Liste = new ArrayList();
e.add(2);
e.add(1);
Listf = new ArrayList();
f.add(1);
f.add(2);
Listg = new ArrayList();
g.add(12);
//比较两集合值
System.out.println(CollectionUtils.isEqualCollection(e,f)); //true
System.out.println(CollectionUtils.isEqualCollection(f,g)); //false
Listh = new ArrayList();
h.add(1);
h.add(2);
h.add(3);;
Listi = new ArrayList();
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]
}
}
CollectionUtils.isEmpty() 判断集合是否为空
StringUtils. isEmpty()
//判断某字符串是否为空,为空的标准是str==null或str.length()==0
//下面是StringUtils判断是否为空的示例:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在StringUtils中空格作非空处理
StringUtils.isBlank(str)
//判断某字符串是否为空或长度为0或由空白符(whitespace)构成
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("\t \n \f \r") = true //对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符
StringUtils.isBlank("\b") = false //"\b"为单词边界符
本文介绍了Apache Commons Collections的CollectionUtils工具类,包括如何判断集合是否为空、不为空,以及执行集合间的并集、交集、差集操作。同时,也探讨了StringUtils类中的isEmpty和isBlank方法,用于检查字符串是否为空或仅包含空白符。

172

被折叠的 条评论
为什么被折叠?



