歡迎關注作者博客
簡書傳送門
前言
論閱讀源碼的重要性,後期會對各大開源框架相關源碼做詳細閱讀,並熟悉使用,本次主要對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.*;
/**
* <p>
* 通過apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具類 對集合間進行合併union、交叉intersection、分離disjunction、減去subtract、任意包含containsAny、
* 判斷是否爲子集isSubCollection、顛倒序列reverseArray及判斷是否填滿isFull等操作。
* </p>
**/
public class CollectionsUtilsDemo {
/** Arrays.asList()返回的是Arrays內部類ArraysList,不可對其進行add、remove等操作,返回報UnsupportedOperationException */
/** java.util.ArrayList和Arrays內部類ArraysList都繼承AbstractList,remove、add等方法AbstractList中是默認throw UnsupportedOperationException而且不作任何操作。*/
/** java.util.ArrayList重新了這些方法而Arrays的內部類ArrayList沒有重新,所以會拋出異常。*/
// @Deprecated
// private static List<String> list1 = Arrays.asList(new String[] {"1", "2", "3", "1"});
// @Deprecated
// private static List<String> list2 = Arrays.asList(new String[] {"2", "3", "4"});
// @Deprecated
// private static List<String> list3 = Arrays.asList(new String[] {"1", "2"});
/** 解決 */
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);
}
/**
* list1 - list2 = 剩餘元素組成的集合
*/
@Test
public void subtractT1() {
// Collection b = CollectionUtils.subtract(list1, list2);
Collection b = CollectionUtils.subtract(list2, list1);
System.out.println(b);
}
/**
* 統計集合中各元素出現的次數,並Map<Object, Integer>輸出
*/
@Test
public void getCardinalityMapT1() {
Map cardinalityMap = CollectionUtils.getCardinalityMap(list1);
cardinalityMap.forEach((k, v) -> System.out.println(k + ":" + v));
}
/**
* a是否b集合子集,a集合大小<=b集合大小
*/
@Test
public void isSubCollectionT1() {
// boolean subCollection = CollectionUtils.isSubCollection(list3, list1);
boolean subCollection = CollectionUtils.isSubCollection(list3, list2);
System.out.println(subCollection);
}
/**
* a是否b集合子集,a集合大小<b集合大小
*/
@Test
public void isProperSubCollectionT1() {
// boolean subCollection = CollectionUtils.isSubCollection(list3, list1);
boolean subCollection = CollectionUtils.isProperSubCollection(list3, list2);
System.out.println(subCollection);
}
/**
* 兩個集合是否相同
*/
@Test
public void isEqualCollectionT1() {
boolean subCollection = CollectionUtils.isSubCollection(list1, list1);
// boolean subCollection = CollectionUtils.isEqualCollection(list3, list2);
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());
}
/**
* 對集合中的對象中的某一屬性進行批量更新,closure爲需要更新的屬性對象
* 如對集合中所有員工的加薪20%
*/
@Test
public void forAllDoT1() {
// // create the closure
// List<Employee> employees = new ArrayList<>();
// Employee e1 = new Employee("e1", "e1.com", 21, 10000, true);
// Employee e2 = new Employee("e2", "e2.com", 22, 14000, false);
// Employee e3 = new Employee("e3", "e3.com", 23, 12000, true);
// Employee e4 = new Employee("e4", "e4.com", 21, 12000, true);
// Closure<E> closure = new Closure() {
// @Override
// public void execute(Employee e) {
// e.setSalary(e.getSalary() * 1.2);
// }
// };
}
/**
* 過濾集合中滿足函數式的所有元素
*/
@Test
public void filterT1() {
CollectionUtils.filter(list1, e -> Integer.parseInt(e.toString()) > 1);
list1.forEach(s -> {
System.out.println(s);
});
}
/**
* 轉換新的集合,對集合中元素進行操作,如每個元素都累加1
*/
@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("============================");
// JDK8
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);
}
/**
* collect底層調用的transform方法
* 將所有元素進行處理,並返回新的集合
*/
@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() {
// List<Integer> boundedList = new ArrayList<>(8);
// int i = CollectionUtils.maxSize(boundedList);
// System.out.println(i);
}
/**
* 只要集合中元素不滿足表達式就拋出異常
*/
@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.predicatedCollection(list1, i -> Integer.parseInt((String)i) > 0);
// Collection collection = CollectionUtils.typedCollection(list1, String.class);
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);
}
}