
- 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 庫的 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]
廣告