目录
7.containsAny()方法:判断两个集合是否有交集,如果存在交集则返回true,否则返回false。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
import org.apache.commons.collections4.CollectionUtils;
@Test
public void Users12() {
List<Users> 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<Users> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA = Arrays.asList(A);
List<String> 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<String> listA2 = Arrays.asList(A2);
List<String> listB2 = Arrays.asList(B2);
if (CollectionUtils.containsAll(listA2,listB2)){
System.out.println("数据完全相同");
}else {
System.out.println("数据有不相同的");
}
}