目录
依赖
这个包下
1.安全检查指定的集合是否为空
2.空 安全检查指定的集合是否为空
3.(String)类型中的集合-并集 union
4.(String)类型中的集合-交集
5.(String)类型中交集的补集
6.(String)类型中差集(扣除)
7.containsAny()方法:判断两个集合是否有交集,如果存在交集则返回true,否则返回false。
8.是否全部包含: containsAll
org.apache.commons
commons-collections4
4.3
import org.apache.commons.collections4.CollectionUtils;
@Test
public void Users12() {
List userList = new ArrayList<>();
userList.add(new Users(1, "user1", "password1", 25));
userList.add(new Users(2, "user2", "password2", 17));
userList.add(new Users(3, "user3", "password3", 19));
userList.add(new Users(4, "user4", "password4", 18));
userList.add(new Users(5, "user5", "password5", 19));
userList.add(new Users(6, "user6", "password6", 20));
/**
* 空 安全检查指定的集合是否为空
*/
//CollectionUtils.isEmpty(Collection> coll)
if(CollectionUtils.isEmpty(userList)){
System.out.println("集合为空");
} else {
System.out.println("集合不为空");
}
}
@Test
public void Users12() {
List userList = new ArrayList<>();
userList.add(new Users(1, "user1", "password1", 25));
userList.add(new Users(2, "user2", "password2", 17));
userList.add(new Users(3, "user3", "password3", 19));
userList.add(new Users(4, "user4", "password4", 18));
userList.add(new Users(5, "user5", "password5", 19));
userList.add(new Users(6, "user6", "password6", 20));
/**
* 空安全检查指定的集合是否为空。
*/
// CollectionUtils.isNotEmpty(Collection> coll)
if(CollectionUtils.isNotEmpty(userList)){
System.out.println("集合不为空");
} else {
System.out.println("集合为空");
}
}
@Test
public void Users12() {
String[] A = new String[] { "A", "2", "3", "4"};
String[] B = new String[] { "3", "C", "5", "A" };
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
//1、并集 union ,就是重复的元素只留一个,本身就是一个的就留
System.out.println(CollectionUtils.union(listA, listB));
}
@Test
public void Users18() {
String[] A = new String[] { "A", "2", "3", "4"};
String[] B = new String[] { "3", "C", "5", "A" };
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
//交集
System.out.println(CollectionUtils.intersection(listA, listB));
}
@Test
public void Users18() {
String[] A = new String[] { "A", "2", "3", "4"};
String[] B = new String[] { "3", "C", "5", "A" };
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
//交集的补集
System.out.println(CollectionUtils.disjunction(listA, listB));
}
@Test
public void Users18() {
String[] A = new String[]{"A", "2", "3", "4"};
String[] B = new String[]{"3", "C", "5", "A"};
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
//4、差集(扣除)
System.out.println(CollectionUtils.subtract(listA, listB));
//输出:[2, 4]
System.out.println(CollectionUtils.subtract(listB, listA));
//输出:[C, 5]
}
@Test
public void Users18() {
String[] A = new String[]{"A", "2", "3", "4"};
String[] B = new String[]{"3", "C", "5", "A"};
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
if (CollectionUtils.containsAny(listA,listB)){
System.out.println("有相同的数据");
}else {
System.out.println("没有相同的数据");
}
}
@Test
public void Users18() {
String[] A = new String[]{"A", "2", "3", "4"};
String[] B = new String[]{"3", "C", "5", "A"};
List listA = Arrays.asList(A);
List listB = Arrays.asList(B);
if (CollectionUtils.containsAll(listA,listB)){
System.out.println("数据完全相同");
}else {
System.out.println("数据有不相同的");
}
System.out.println("-----------------------------------------");
String[] A2 = new String[]{"A", "3"};
String[] B2 = new String[]{"3", "A"};
List listA2 = Arrays.asList(A2);
List listB2 = Arrays.asList(B2);
if (CollectionUtils.containsAll(listA2,listB2)){
System.out.println("数据完全相同");
}else {
System.out.println("数据有不相同的");
}
}