欢迎关注作者博客 简书传送门
文章目录
前言代码示例
前言
论阅读源码的重要性,后期会对各大开源框架相关源码做详细阅读,并熟悉使用,本次主要对Apache Commons Collections中CollectionUtils类进行示例分析,如有错误,请多指教。
通过apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具类 对集合间进行合并union、交叉intersection、分离disjunction、减去subtract、任意包含containsAny、判断是否为子集isSubCollection、颠倒序列reverseArray及判断是否填满isFull等操作。
代码示例
package com
.zzx
.apache
.commons
.chapter1
;
import lombok
.Data
;
import org
.apache
.commons
.collections
.CollectionUtils
;
import org
.apache
.commons
.collections
.Transformer
;
import org
.junit
.jupiter
.api
.Test
;
import java
.util
.*
;
public class CollectionsUtilsDemo {
private static List
<String> list1
= new ArrayList<>(Arrays
.asList(new String[] {"1", "2", "3", "1", "5"}));
private static List
<String> list2
= new ArrayList<>(Arrays
.asList(new String[] {"2", "3", "1"}));
private static List
<String> list3
= new ArrayList<>(Arrays
.asList(new String[] {"1", "2"}));
@Data
class Employee {
private String name
;
private String email
;
private int age
;
private double salary
;
private boolean activeEmployee
;
public Employee(String name
, String email
, int age
, double salary
, boolean activeEmployee
) {
this.name
= name
;
this.email
= email
;
this.age
= age
;
this.salary
= salary
;
this.activeEmployee
= activeEmployee
;
}
}
public void containsAnyT1() {
boolean b
= CollectionUtils
.containsAny(list1
, list2
);
System
.out
.println(b
);
}
@Test
public void intersectionT1() {
Collection b
= CollectionUtils
.intersection(list1
, list2
);
System
.out
.println(b
);
}
@Test
public void unionT1() {
Collection b
= CollectionUtils
.union(list1
, list2
);
System
.out
.println(b
);
}
@Test
public void disjunctionT1() {
Collection b
= CollectionUtils
.disjunction(list1
, list2
);
System
.out
.println(b
);
}
@Test
public void subtractT1() {
Collection b
= CollectionUtils
.subtract(list2
, list1
);
System
.out
.println(b
);
}
@Test
public void getCardinalityMapT1() {
Map cardinalityMap
= CollectionUtils
.getCardinalityMap(list1
);
cardinalityMap
.forEach((k
, v
) -> System
.out
.println(k
+ ":" + v
));
}
@Test
public void isSubCollectionT1() {
boolean subCollection
= CollectionUtils
.isSubCollection(list3
, list2
);
System
.out
.println(subCollection
);
}
@Test
public void isProperSubCollectionT1() {
boolean subCollection
= CollectionUtils
.isProperSubCollection(list3
, list2
);
System
.out
.println(subCollection
);
}
@Test
public void isEqualCollectionT1() {
boolean subCollection
= CollectionUtils
.isSubCollection(list1
, list1
);
System
.out
.println(subCollection
);
}
@Test
public void cardinalityT1() {
int cardinality
= CollectionUtils
.cardinality("1", list1
);
System
.out
.println(cardinality
);
}
@Test
public void findT1() {
Object o
= CollectionUtils
.find(list1
, e
-> Integer
.parseInt(e
.toString()) > 2);
System
.out
.println(o
.toString());
}
@Test
public void forAllDoT1() {
}
@Test
public void filterT1() {
CollectionUtils
.filter(list1
, e
-> Integer
.parseInt(e
.toString()) > 1);
list1
.forEach(s
-> {
System
.out
.println(s
);
});
}
@Test
public void transformT1() {
CollectionUtils
.transform(list1
, new Transformer() {
@Override
public Object
transform(Object o
) {
Integer num
= Integer
.parseInt((String
)o
);
return String
.valueOf(++num
);
}
});
list1
.forEach(s
-> {
System
.out
.println(s
);
});
System
.out
.println("============================");
List
<String> temp
= new ArrayList<>();
list1
.stream().forEach(i
-> {
int num
= Integer
.parseInt(i
);
temp
.add(String
.valueOf(num
));
});
temp
.forEach(System
.out
::println
);
}
@Test
public void countMatchesT1() {
int num
= CollectionUtils
.countMatches(list1
, i
-> Integer
.parseInt((String
)i
) > 0);
System
.out
.println(num
);
}
@Test
public void selectT1() {
Collection select
= CollectionUtils
.select(list1
, i
-> Integer
.parseInt((String
)i
) > 2);
select
.forEach(System
.out
::println
);
}
@Test
public void selectRejectedT1() {
Collection select
= CollectionUtils
.selectRejected(list1
, i
-> Integer
.parseInt((String
)i
) > 2);
select
.forEach(System
.out
::println
);
}
@Test
public void collectT1() {
Collection collecttion
= CollectionUtils
.collect(list1
, new Transformer() {
@Override
public Object
transform(Object o
) {
int i
= Integer
.parseInt((String
)o
);
return ++i
;
}
});
collecttion
.forEach(System
.out
::println
);
}
@Test
public void adAllT1() {
CollectionUtils
.addAll(list1
, new String[]{"5", "6"});
CollectionUtils
.addAll(list1
, list2
.toArray());
list1
.forEach(System
.out
::println
);
}
@Test
public void indexT1() {
String index
= (String
)CollectionUtils
.index(list1
, 2);
System
.out
.println(index
);
}
@Test
public void getT1() {
String index
= (String
)CollectionUtils
.get(list1
, 2);
System
.out
.println(index
);
}
@Test
public void isEmptyT1() {
int[] arr
= new int[5];
arr
[0] = 1;
arr
[1] = 1;
arr
[2] = 1;
arr
[3] = 1;
arr
[4] = 1;
boolean empty
= CollectionUtils
.isFull(new ArrayList(Arrays
.asList(arr
)));
System
.out
.println(empty
);
}
@Test
public void isFullT1() {
boolean full
= CollectionUtils
.isFull(list1
);
System
.out
.println(full
);
}
@Test
public void maxSizeT1() {
}
@Test
public void predicatedCollectionT1() {
Collection collection
= CollectionUtils
.predicatedCollection(list1
, i
-> Integer
.parseInt((String
)i
) > 1);
collection
.forEach(System
.out
::println
);
}
@Test
public void removeAllT1() {
boolean b
= list1
.removeAll(list2
);
System
.out
.println(b
);
list1
.forEach(System
.out
::println
);
}
@Test
public void synchronizedCollectionT1() {
Collection collection
= CollectionUtils
.synchronizedCollection(list1
);
collection
.forEach(System
.out
::println
);
}
@Test
public void unmodifiedCollectionT1() {
Collection collection
= CollectionUtils
.unmodifiableCollection(list1
);
collection
.forEach(System
.out
::println
);
}
@Test
public void predicatedCollectionT2() {
Collection collection
= CollectionUtils
.transformedCollection(list1
, new Transformer() {
@Override
public Object
transform(Object o
) {
int n
= Integer
.parseInt((String
)o
);
return n
+ n
;
}
});
collection
.forEach(System
.out
::println
);
}
}
欢迎加入Java猿社区!
免费领取我历年收集的所有学习资料哦!