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;
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
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
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
//引入的包
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
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);
}