目录
2.空 安全检查指定的集合是否不为空(isNotEmpty)
4.(String)类型中的集合-交集(intersection)
5.(String)类型中交集的补集(disjunction)
7.containsAny()方法:判断两个集合是否有交集,如果存在交集则返回true,否则返回false。
9.getCardinalityMap(这个方法的作用是统计集合中每个元素出现的次数,并以映射的形式返回。)
依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
这个包下
import org.apache.commons.collections4.CollectionUtils;
1.安全检查指定的集合是否为空(isEmpty)
@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("集合不为空");
}
}
2.空 安全检查指定的集合是否不为空(isNotEmpty)
@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("集合为空");
}
}
3.(String)类型中的集合-并集 union
@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));
}
4.(String)类型中的集合-交集(intersection)
@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));
}
5.(String)类型中交集的补集(disjunction)
@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));
}
6.(String)类型中差集(扣除)(subtract)
@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]
}
7.containsAny()方法:判断两个集合是否有交集,如果存在交集则返回true,否则返回false。
@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("没有相同的数据");
}
}
8.是否全部包含: containsAll
@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("数据有不相同的");
}
}
9.getCardinalityMap(这个方法的作用是统计集合中每个元素出现的次数,并以映射的形式返回。)
@Test
public void test1(){
List<String> emptyList = new ArrayList<>();
Map<String, Integer> emptyMap = CollectionUtils.getCardinalityMap(emptyList);
System.out.println(emptyMap); // 输出: {}
List<String> list = Arrays.asList("apple", "banana", "apple", "orange", "banana", "banana");
Map<String, Integer> countMap = CollectionUtils.getCardinalityMap(list);
System.out.println(countMap); // 输出: {apple=2, banana=3, orange=1}
List<Integer> list1 = Arrays.asList(1, 2, 3, 3, 2, 1);
Map<Integer, Integer> countMap2 = CollectionUtils.getCardinalityMap(list1);
System.out.println(countMap2); // 输出: {1=2, 2=2, 3=2}
}