
- Commons Collections 教程
- Commons Collections - 首頁
- Commons Collections - 概述
- Commons Collections - 環境設定
- Commons Collections - Bag 介面
- Commons Collections - BidiMap 介面
- Commons Collections - MapIterator 介面
- Commons Collections - OrderedMap 介面
- Commons Collections - 忽略空值
- Commons Collections - 合併與排序
- Commons Collections - 物件轉換
- Commons Collections - 物件過濾
- Commons Collections - 安全的空檢查
- Commons Collections - 包含
- Commons Collections - 交集
- Commons Collections - 差集
- Commons Collections - 並集
- Commons Collections 資源
- Commons Collections - 快速指南
- Commons Collections - 有用資源
- Commons Collections - 討論
Apache Commons Collections 快速指南
Apache Commons Collections - 概述
Commons Collections 增強了 Java Collections Framework。它提供了一些特性,使集合處理更加容易。它提供了許多新的介面、實現和實用工具。
Commons Collections 的主要功能如下:
Bag − Bag 介面簡化了集合,這些集合包含多個物件的多個副本。
BidiMap − BidiMap 介面提供雙向對映,可用於使用鍵查詢值或使用值查詢鍵。
MapIterator − MapIterator 介面提供對對映的簡單易迭代。
轉換裝飾器 − 轉換裝飾器可以隨時更改新增到集合中的每個物件。
組合集合 − 當需要統一處理多個集合時,使用組合集合。
有序對映 − 有序對映保留元素新增的順序。
有序集合 − 有序集合保留元素新增的順序。
引用對映 − 引用對映允許在嚴格控制下進行鍵/值的垃圾回收。
比較器實現 − 提供了許多比較器實現。
迭代器實現 − 提供了許多迭代器實現。
介面卡類 − 提供了將陣列和列舉轉換為集合的介面卡類。
實用工具 − 提供了用於測試或建立集合的典型集合論屬性(如並集、交集)的實用工具。支援閉包。
Commons Collections - 環境設定
本地環境設定
如果您仍然希望為 Java 程式語言設定環境,那麼本節將指導您如何在機器上下載和設定 Java。請按照下面提到的步驟設定環境。
Java SE 可從以下連結免費獲得:www.oracle.com/technetwork/java/archive-139210.html 因此,您可以根據您的作業系統下載一個版本。
按照說明下載 Java 並執行 .exe 檔案以在您的機器上安裝 Java。一旦您在機器上安裝了 Java,您需要設定環境變數以指向正確的安裝目錄。
為 Windows 2000/XP 設定路徑
我們假設您已將 Java 安裝在 c:\Program Files\java\jdk 目錄
右鍵單擊“我的電腦”,然後選擇“屬性”。
在“高階”選項卡下單擊“環境變數”按鈕。
現在,更改“Path”變數,使其還包含 Java 可執行檔案的路徑。例如,如果路徑當前設定為 'C:\WINDOWS\SYSTEM32',則將路徑更改為 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'。
為 Windows 95/98/ME 設定路徑
我們假設您已將 Java 安裝在 c:\Program Files\java\jdk 目錄。
編輯 'C:\autoexec.bat' 檔案,並在末尾新增以下行:'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'。
為 Linux、UNIX、Solaris、FreeBSD 設定路徑
應將環境變數 PATH 設定為指向 Java 二進位制檔案安裝的位置。如果您遇到問題,請參考您的 shell 文件。
例如,如果您使用 bash 作為您的 shell,則您將在 '.bashrc: export PATH=/path/to/java:$PATH' 的末尾新增以下行
常用的 Java 編輯器
要編寫 Java 程式,您需要一個文字編輯器。市場上有很多複雜的 IDE。但目前,您可以考慮以下其中一種:
記事本 − 在 Windows 機器上,您可以使用任何簡單的文字編輯器,例如記事本(推薦用於本教程)、TextPad。
Netbeans − 它是一個開源且免費的 Java IDE,可以從 www.netbeans.org/index.html 下載。
Eclipse − 它也是由 Eclipse 開源社群開發的 Java IDE,可以從 www.eclipse.org 下載。
下載 Commons Collections 存檔
從 commons-collections4-4.1-bin.zip 下載 Apache Common Collections jar 檔案的最新版本。在撰寫本教程時,我們已下載 commons-collections4-4.1-bin.zip 並將其複製到 C:\>Apache 資料夾。
作業系統 | 存檔名稱 |
---|---|
Windows | commons-collections4-4.1-bin.zip |
Linux | commons-collections4-4.1-bin.tar.gz |
Mac | commons-collections4-4.1-bin.tar.gz |
設定 Apache Common Collections 環境
設定 APACHE_HOME 環境變數以指向 Apache jar 儲存在您機器上的基目錄位置。假設我們在各個作業系統上的 Apache 資料夾中解壓了 commons-collections4-4.1-bin.zip,如下所示:
作業系統 | 存檔名稱 |
---|---|
Windows | 將環境變數 APACHE_HOME 設定為 C:\Apache |
Linux | export APACHE_HOME=/usr/local/Apache |
Mac | export APACHE_HOME=/Library/Apache |
設定 CLASSPATH 變數
設定 CLASSPATH 環境變數以指向 Common Collections jar 的位置。假設您已將 commons-collections4-4.1-bin.zip 儲存在各個作業系統的 Apache 資料夾中,如下所示:
作業系統 | 輸出 |
---|---|
Windows | 將環境變數 CLASSPATH 設定為 %CLASSPATH%;%APACHE_HOME%\commons-collections4-4.1-bin.jar;.; |
Linux | export CLASSPATH=$CLASSPATH:$APACHE_HOME/commons-collections4-4.1-bin.jar:. |
Mac | export CLASSPATH=$CLASSPATH:$APACHE_HOME/commons-collections4-4.1-bin.jar:. |
Apache Commons Collections - Bag 介面
添加了新的介面來支援包。包定義了一個集合,它計算物件在集合中出現的次數。例如,如果一個包包含 {a, a, b, c},則 getCount("a") 將返回 2,而 uniqueSet() 將返回唯一值。
介面宣告
以下是 org.apache.commons.collections4.Bag<E> 介面的宣告:
public interface Bag<E> extends Collection<E>
方法
包介面的方法如下:
序號 | 方法和描述 |
---|---|
1 | boolean add(E object) (違規) 將指定物件的副本新增到包中。 |
2 | boolean add(E object, int nCopies) 將指定物件的 nCopies 個副本新增到包中。 |
3 | boolean containsAll(Collection<?> coll) (違規) 如果包包含給定集合中的所有元素(考慮基數),則返回 true。 |
4 | int getCount(Object object) 返回包中給定物件的出現次數(基數)。 |
5 | Iterator<E> iterator() 返回整個成員集(包括由於基數而導致的副本)上的迭代器。 |
6 | boolean remove(Object object) (違規) 從包中刪除給定物件的所有出現。 |
7 | boolean remove(Object object, int nCopies) 從包中刪除指定物件的 nCopies 個副本。 |
8 | boolean removeAll(Collection<?> coll) (違規) 刪除給定集合中表示的所有元素(考慮基數)。 |
9 | boolean retainAll(Collection<?> coll) (違規) 刪除包中不在給定集合中的任何成員(考慮基數)。 |
10 | int size() 返回包中所有型別專案的總數。 |
11 | Set<E> uniqueSet() 返回包中唯一元素的集合。 |
繼承的方法
此介面繼承自以下介面的方法:
- java.util.Collectio。
Bag 介面示例
BagTester.java 的示例如下:
import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.bag.HashBag; public class BagTester { public static void main(String[] args) { Bag<String> bag = new HashBag<>(); //add "a" two times to the bag. bag.add("a" , 2); //add "b" one time to the bag. bag.add("b"); //add "c" one time to the bag. bag.add("c"); //add "d" three times to the bag. bag.add("d",3 //get the count of "d" present in bag. System.out.println("d is present " + bag.getCount("d") + " times."); System.out.println("bag: " +bag); //get the set of unique values from the bag System.out.println("Unique Set: " +bag.uniqueSet()); //remove 2 occurrences of "d" from the bag bag.remove("d",2); System.out.println("2 occurences of d removed from bag: " +bag); System.out.println("d is present " + bag.getCount("d") + " times."); System.out.println("bag: " +bag); System.out.println("Unique Set: " +bag.uniqueSet()); } }
輸出
您將看到以下輸出:
d is present 3 times. bag: [2:a,1:b,1:c,3:d] Unique Set: [a, b, c, d] 2 occurences of d removed from bag: [2:a,1:b,1:c,1:d] d is present 1 times. bag: [2:a,1:b,1:c,1:d] Unique Set: [a, b, c, d]
Commons Collections - BidiMap 介面
添加了新的介面來支援雙向對映。使用雙向對映,可以使用值查詢鍵,也可以使用鍵輕鬆查詢值。
介面宣告
以下是 org.apache.commons.collections4.BidiMap<K,V> 介面的宣告:
public interface BidiMap<K,V> extends IterableMap<K,V>
方法
BidiMap 介面的方法如下:
序號 | 方法和描述 |
---|---|
1 | K getKey(Object value) 獲取當前對映到指定值的鍵。 |
2 | BidiMap<V,K> inverseBidiMap() 獲取此對映的檢視,其中鍵和值被反轉。 |
3 | V put(K key, V value) 將鍵值對放入對映中,替換任何之前的對。 |
4 | K removeValue(Object value) 刪除當前對映到指定值的鍵值對(可選操作)。 |
5 | Set<V> values() 返回此對映中包含的值的 Set 檢視。 |
繼承的方法
此介面繼承自以下介面的方法:
org.apache.commons.collections4.Ge。
org.apache.commons.collections4.IterableGe。
org.apache.commons.collections4.Pu。
java.util.Ma。
BidiMap 介面示例
BidiMapTester.java 的示例如下:
import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.bidimap.TreeBidiMap; public class BidiMapTester { public static void main(String[] args) { BidiMap>String, String< bidi = new TreeBidiMap<>(); bidi.put("One", "1"); bidi.put("Two", "2"); bidi.put("Three", "3"); System.out.println(bidi.get("One")); System.out.println(bidi.getKey("1")); System.out.println("Original Map: " + bidi); bidi.removeValue("1"); System.out.println("Modified Map: " + bidi); BidiMap<String, String> inversedMap = bidi.inverseBidiMap(); System.out.println("Inversed Map: " + inversedMap); } }
輸出
執行程式碼時,您將看到以下輸出:
1 One Original Map: {One=1, Three=3, Two=2} Modified Map: {Three=3, Two=2} Inversed Map: {2=Two, 3=Three}
Commons Collections - MapIterator 介面
JDK Map 介面很難迭代,因為迭代需要在 EntrySet 或 KeySet 物件上進行。MapIterator 提供對 Map 的簡單迭代。以下示例說明了這一點。
MapIterator 介面示例
MapIteratorTester.java 的示例如下:
import org.apache.commons.collections4.IterableMap; import org.apache.commons.collections4.MapIterator; import org.apache.commons.collections4.map.HashedMap; public class MapIteratorTester { public static void main(String[] args) { IterableMap<String, String> map = new HashedMap<>(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); MapIterator<String, String> iterator = map.mapIterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = iterator.getValue(); System.out.println("key: " + key); System.out.println("Value: " + value); iterator.setValue(value + "_"); } System.out.println(map); } }
輸出
輸出如下:
key: 3 Value: Three key: 5 Value: Five key: 2 Value: Two key: 4 Value: Four key: 1 Value: One {3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_}
Commons Collections - OrderedMap 介面
OrderedMap 是一個用於對映的新介面,用於保留元素新增的順序。LinkedMap 和 ListOrderedMap 是兩個可用的實現。此介面支援 Map 的迭代器,並允許在 Map 中向前或向後兩個方向進行迭代。以下示例說明了這一點。
MapIterator 介面示例
OrderedMapTester.java 的示例如下:
import org.apache.commons.collections4.OrderedMap; import org.apache.commons.collections4.map.LinkedMap; public class OrderedMapTester { public static void main(String[] args) { OrderedMap<String, String> map = new LinkedMap<String, String>(); map.put("One", "1"); map.put("Two", "2"); map.put("Three", "3"); System.out.println(map.firstKey()); System.out.println(map.nextKey("One")); System.out.println(map.nextKey("Two")); } }
輸出
結果如下:
One Two Three
Apache Commons Collections - 忽略空值
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查非空元素
CollectionUtils的addIgnoreNull()方法可以確保只有非空值被新增到集合中。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.addIgnoreNull() 方法的宣告:
public static <T> boolean addIgnoreNull(Collection<T> collection, T object)
引數
collection − 要新增到的集合,不能為null。
object − 要新增的物件,如果為null,則不會新增。
返回值
如果集合發生更改,則返回true。
異常
NullPointerException − 如果集合為null。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.addIgnoreNull() 方法的使用。我們將嘗試新增一個null值和一個示例非空值。
import java.util.LinkedList; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = new LinkedList<String>(); CollectionUtils.addIgnoreNull(list, null); CollectionUtils.addIgnoreNull(list, "a"); System.out.println(list); if(list.contains(null)) { System.out.println("Null value is present"); } else { System.out.println("Null value is not present"); } } }
輸出
輸出如下所示:
[a] Null value is not present
Apache Commons Collections - 合併與排序
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
合併兩個已排序的列表
CollectionUtils的collate()方法可以用來合併兩個已經排序的列表。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.collate() 方法:
public static <O extends Comparable<? super O>> List<O> collate(Iterable<? extends O> a, Iterable<? extends O> b)
引數
a − 第一個集合,不能為null。
b − 第二個集合,不能為null。
返回值
一個新的已排序列表,包含集合a和b的元素。
異常
NullPointerException − 如果任一集合為null。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.collate() 方法的使用。我們將合併兩個已排序的列表,然後打印合並後的已排序列表。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { 8. Apache Commons Collections — Merge & Sort public static void main(String[] args) { List<String> sortedList1 = Arrays.asList("A","C","E"); List<String> sortedList2 = Arrays.asList("B","D","F"); List<String> mergedList = CollectionUtils.collate(sortedList1, sortedList2); System.out.println(mergedList); } }
輸出
輸出如下:
[A, B, C, D, E, F]
Commons Collections - 物件轉換
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
轉換列表
CollectionUtils的collect()方法可以用來將一個型別物件的列表轉換為不同型別物件的列表。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.collect() 方法:
public static <I,O> Collection<O> collect(Iterable<I> inputCollection, Transformer<? super I,? extends O> transformer)
引數
inputCollection − 獲取輸入的集合,不能為null。
Transformer − 要使用的轉換器,可以為null。
返回值
轉換後的結果(新列表)。
異常
NullPointerException − 如果輸入集合為null。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.collect() 方法的使用。我們將透過解析字串中的整數值,將字串列表轉換為整數列表。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Transformer; public class CollectionUtilsTester { public static void main(String[] args) { List<String> stringList = Arrays.asList("1","2","3"); List<Integer> integerList = (List<Integer>) CollectionUtils.collect(stringList, new Transformer<String, Integer>() { @Override public Integer transform(String input) { return Integer.parseInt(input); } }); System.out.println(integerList); } }
輸出
使用程式碼時,您將獲得以下程式碼:
[1, 2, 3]
Commons Collections - 物件過濾
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
filter() 方法
CollectionUtils的filter()方法可以用來過濾列表,刪除不滿足傳遞的謂詞提供的條件的物件。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.filter() 方法:
public static <T> boolean filter(Iterable<T> collection, Predicate<? super T> predicate)
引數
collection − 獲取輸入的集合,不能為null。
predicate − 用作過濾器的謂詞,可以為null。
返回值
如果此呼叫修改了集合,則返回true,否則返回false。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.filter() 方法的使用。我們將過濾整數列表以僅獲取偶數。
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; public class CollectionUtilsTester { public static void main(String[] args) { List<Integer> integerList = new ArrayList<Integer>(); integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8)); System.out.println("Original List: " + integerList); CollectionUtils.filter(integerList, new Predicate<Integer>() { @Override public boolean evaluate(Integer input) { if(input.intValue() % 2 == 0) { return true; } return false; } }); System.out.println("Filtered List (Even numbers): " + integerList); } }
輸出
它將產生以下結果:
Original List: [1, 2, 3, 4, 5, 6, 7, 8] Filtered List (Even numbers): [2, 4, 6, 8]
filterInverse() 方法
CollectionUtils的filterInverse()方法可以用來過濾列表,刪除滿足傳遞的謂詞提供的條件的物件。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.filterInverse() 方法:
public static <T> boolean filterInverse(Iterable<T> collection, Predicate<? super T> predicate)
引數
collection − 獲取輸入的集合,不能為null。
predicate − 用作過濾器的謂詞,可以為null。
返回值
如果此呼叫修改了集合,則返回true,否則返回false。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.filterInverse() 方法的使用。我們將過濾整數列表以僅獲取奇數。
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; public class CollectionUtilsTester { public static void main(String[] args) { List<Integer> integerList = new ArrayList<Integer>(); integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8)); System.out.println("Original List: " + integerList); CollectionUtils.filterInverse(integerList, new Predicate<Integer>() { @Override public boolean evaluate(Integer input) { if(input.intValue() % 2 == 0) { return true; } return false; } }); System.out.println("Filtered List (Odd numbers): " + integerList); } }
輸出
結果如下所示:
Original List: [1, 2, 3, 4, 5, 6, 7, 8] Filtered List (Odd numbers): [1, 3, 5, 7]
Commons Collections - 安全的空檢查
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查非空列表
CollectionUtils的isNotEmpty()方法可以用來檢查列表是否非空,而無需擔心null列表。因此,在檢查列表的大小之前,無需在任何地方都放置null檢查。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.isNotEmpty() 方法:
public static boolean isNotEmpty(Collection<?> coll)
引數
coll − 要檢查的集合,可以為null。
返回值
如果非空且非null,則返回true。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.isNotEmpty() 方法的使用。我們將檢查列表是否為空。
import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = getList(); System.out.println("Non-Empty List Check: " + checkNotEmpty1(list)); System.out.println("Non-Empty List Check: " + checkNotEmpty1(list)); } static List<String> getList() { return null; } static boolean checkNotEmpty1(List<String> list) { return !(list == null || list.isEmpty()); } static boolean checkNotEmpty2(List<String> list) { return CollectionUtils.isNotEmpty(list); } }
輸出
輸出如下所示:
Non-Empty List Check: false Non-Empty List Check: false
檢查空列表
CollectionUtils的isEmpty()方法可以用來檢查列表是否為空,而無需擔心null列表。因此,在檢查列表的大小之前,無需在任何地方都放置null檢查。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.isEmpty() 方法:
public static boolean isEmpty(Collection<?> coll)
引數
coll − 要檢查的集合,可以為null。
返回值
如果為空或null,則返回true。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.isEmpty() 方法的使用。我們將檢查列表是否為空。
import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = getList(); System.out.println("Empty List Check: " + checkEmpty1(list)); System.out.println("Empty List Check: " + checkEmpty1(list)); } static List<String> getList() { return null; } static boolean checkEmpty1(List<String> list) { return (list == null || list.isEmpty()); } static boolean checkEmpty2(List<String> list) { return CollectionUtils.isEmpty(list); } }
輸出
以下是程式碼的輸出:
Empty List Check: true Empty List Check: true
Apache Commons Collections - 包含
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查子列表
CollectionUtils的isSubCollection()方法可以用來檢查集合是否包含給定的集合。
宣告
以下是
org.apache.commons.collections4.CollectionUtils.isSubCollection() 方法:
public static boolean isSubCollection(Collection<?> a, Collection<?> b)
引數
a − 第一個(子)集合,不能為null。
b − 第二個(父)集合,不能為null。
返回值
當且僅當a是b的子集合時,返回true。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.isSubCollection() 方法的使用。我們將檢查一個列表是否是另一個列表的一部分。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList("A","A","A","C","B","B"); List<String> list2 = Arrays.asList("A","A","B","B"); System.out.println("List 1: " + list1); System.out.println("List 2: " + list2); System.out.println("Is List 2 contained in List 1: " + CollectionUtils.isSubCollection(list2, list1)); } }
輸出
您將收到以下輸出:
List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] Is List 2 contained in List 1: true
Apache Commons Collections - 交集
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查交集
CollectionUtils的intersection()方法可以用來獲取兩個集合之間的公共物件(交集)。
宣告
以下是org.apache.commons.collections4.CollectionUtils.intersection() 方法的宣告:
public static <O> Collection<O> intersection(Iterable<? extends O> a, Iterable<? extends O> b)
引數
a − 第一個(子)集合,不能為null。
b − 第二個(父)集合,不能為null。
返回值
兩個集合的交集。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.intersection() 方法的使用。我們將獲取兩個列表的交集。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList("A","A","A","C","B","B"); List<String> list2 = Arrays.asList("A","A","B","B"); System.out.println("List 1: " + list1); System.out.println("List 2: " + list2); System.out.println("Commons Objects of List 1 and List 2: " + CollectionUtils.intersection(list1, list2)); } }
輸出
執行程式碼時,您將看到以下輸出:
List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] Commons Objects of List 1 and List 2: [A, A, B, B]
Apache Commons Collections - 差集
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查差集
CollectionUtils的subtract()方法可以用來透過從另一個集合中減去一個集合的物件來獲取新的集合。
宣告
以下是org.apache.commons.collections4.CollectionUtils.subtract() 方法的宣告:
public static <O> Collection<O> subtract(Iterable<? extends O> a, Iterable<? extends O> b)
引數
a − 要從中減去的集合,不能為null。
b − 要減去的集合,不能為null。
返回值
一個包含結果的新集合。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.subtract() 方法的使用。我們將獲取兩個列表的差集。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList("A","A","A","C","B","B"); List<String> list2 = Arrays.asList("A","A","B","B"); System.out.println("List 1: " + list1); System.out.println("List 2: " + list2); System.out.println("List 1 - List 2: "+ CollectionUtils.subtract(list1, list2)); } }
輸出
執行上述程式碼後,您應該看到以下輸出:
List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] List 1 - List 2: [A, C]
Apache Commons Collections - 並集
Apache Commons Collections 庫的 CollectionUtils 類為涵蓋廣泛用例的常見操作提供了各種實用程式方法。它有助於避免編寫樣板程式碼。在 JDK 8 之前,此庫非常有用,因為 Java 8 的 Stream API 現在提供了類似的功能。
檢查並集
CollectionUtils的union()方法可以用來獲取兩個集合的並集。
宣告
以下是org.apache.commons.collections4.CollectionUtils.union() 方法的宣告:
public static <O> Collection<O> union(Iterable<? extends O> a, Iterable<? extends O> b)
引數
a − 第一個集合,不能為null。
b − 第二個集合,不能為null。
返回值
兩個集合的並集。
示例
以下示例演示了org.apache.commons.collections4.CollectionUtils.union() 方法的使用。我們將獲取兩個列表的並集。
import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList("A","A","A","C","B","B"); List<String> list2 = Arrays.asList("A","A","B","B"); System.out.println("List 1: " + list1); System.out.println("List 2: " + list2); System.out.println("Union of List 1 and List 2: "+ CollectionUtils.union(list1, list2)); } }
輸出
這將產生以下輸出:
List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] Union of List 1 and List 2: [A, A, A, B, B, C]