目录

Commons Collections - 快速指南

Apache Commons Collections - Overview

Commons Collections增强了Java Collections Framework。 它提供了几个功能,使收集处理变得容易。 它提供了许多新的接口,实现和实用程序。 Commons Collections的主要功能如下 -

  • Bag - Bag界面简化了每个对象具有多个副本的集合。

  • BidiMap - BidiMap接口提供双向映射,可用于使用值使用键或键查找值。

  • MapIterator - MapIterator接口提供简单而容易的迭代迭代。

  • Transforming Decorators - 转换装饰器可以在将集合添加到集合时更改集合的每个对象。

  • Composite Collections - 在需要统一处理多个集合的情况下使用复合集合。

  • Ordered Map - 有序地图保留添加元素的顺序。

  • Ordered Set - 有序集保留了添加元素的顺序。

  • Reference map - 参考图允许在密切控制下对键/值进行垃圾收集。

  • Comparator implementations - 可以使用许多Comparator实现。

  • Iterator implementations - 许多Iterator实现都可用。

  • Adapter Classes - 适配器类可用于将数组和枚举转换为集合。

  • Utilities - 实用程序可用于测试测试或创建集合的典型集合论属性,例如union,intersection。 支持关闭。

Commons Collections - Environment Setup

本地环境设置 (Local Environment Setup)

如果您仍然愿意为Java编程语言设置环境,那么本节将指导您如何在计算机上下载和设置Java。 请按照下面提到的步骤设置环境。

Java SE可从链接Download Java免费获得。 因此,您下载基于您的操作系统的版本。

按照说明下载Java并运行.exe以在您的计算机上安装Java。 在计算机上安装Java后,需要设置环境变量以指向正确的安装目录 -

设置Windows 2000/XP的路径

我们假设您已在c:\Program Files\java\jdk目录中安装了Java -

  • 右键单击“我的电脑”,然后选择“属性”。

  • 单击“高级”选项卡下的“环境变量”按钮。

  • 现在,更改'Path'变量,使其也包含Java可执行文件的路径。 例如,如果路径当前设置为“C:\WINDOWS\SYSTEM32”,则将路径更改为“C:\WINDOWS\SYSTEM32; c:\Program Files\java\jdk\bin”。

设置Windows 95/98/ME的路径

我们假设您已在c:\Program Files\java\jdk目录中安装了Java -

  • 编辑'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编辑器 (Popular Java Editors)

要编写Java程序,需要一个文本编辑器。 市场上有许多复杂的IDE。 但就目前而言,您可以考虑以下其中一项 -

  • Notepad - 在Windows机器上,您可以使用任何简单的文本编辑器,如记事本(本教程推荐),TextPad。

  • Netbeans - 它是一个开源和免费的Java IDE,可以从https://www.netbeans.org/index.html下载。

  • Eclipse - 它也是由eclipse开源社区开发的Java IDE,可以从https://www.eclipse.org/下载。

下载Common Collections Archive

从commons-collections4-4.1-bin.zip下载最新版本的Apache Common Collections jar文件。 在编写本教程时,我们已经下载了commons-collections4-4.1-bin.zip并将其复制到C:\“Apache文件夹中。

OS 存档名称
Windowscommons-collections4-4.1-bin.zip
Linuxcommons-collections4-4.1-bin.tar.gz
Maccommons-collections4-4.1-bin.tar.gz

设置Apache公共集合环境

APACHE_HOME环境变量设置为指向Apache jar存储在计算机上的基本目录位置。 假设我们在各种操作系统的Ap​​ache文件夹中提取了commons-collections4-4.1-bin.zip,如下所示。

OS output
Windows 将环境变量APACHE_HOME设置为C:\Apache
Linux export APACHE_HOME =/usr/local/Apache
Mac export APACHE_HOME =/Library/Apache

设置类路径变量 (Set CLASSPATH Variable)

CLASSPATH环境变量设置为指向Common Collections jar位置。 假设您已将commons-collections4-4.1-bin.zip存储在各种操作系统的Ap​​ache文件夹中,如下所示。

OS output
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 Interface

新的界面被添加到支持袋。 Bag定义了一个集合,用于计算对象在集合中出现的次数。 例如,如果Bag包含{a,a,b,c},则getCount(“a”)将返回2,而uniqueSet()将返回唯一值。

接口声明 (Interface Declaration)

以下是org.apache.commons.collections4.Bag 界面的声明 -

public interface Bag<E>
   extends Collection<E>

方法 (Methods)

Sr.No. 方法和描述
1

boolean add(E object)

(违规)将指定对象的一个​​副本添加到Bag。

2

boolean add(E object, int nCopies)

将指定对象的nCopies副本添加到Bag。

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)

从Bag中删除指定对象的nCopies副本。

8

boolean removeAll(Collection《?》 coll)

(违规)删除给定集合中表示的所有元素,尊重基数。

9

boolean retainAll(Collection《?》 coll)

(违规)根据基数删除不在给定集合中的任何行李成员。

10

int size()

返回所有类型的包中的项目总数。

11

Set《E》 uniqueSet()

返回Bag中的一组唯一元素。

方法继承 (Methods Inherited)

此接口从以下接口继承方法 -

  • java.util.Collection

袋接口示例

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());
   }
}

输出 (Output)

它将打印以下结果。

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 Interface

添加了新接口以支持双向Map。 使用双向映射,可以使用值查找键,并且可以使用键轻松查找值。

接口声明 (Interface Declaration)

以下是org.apache.commons.collections4.BidiMap 接口的声明 -

public interface BidiMap<K,V>
   extends IterableMap<K,V>

方法 (Methods)

Sr.No. 方法和描述
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视图。

方法继承 (Methods Inherited)

此接口从以下接口继承方法 -

  • org.apache.commons.collections4.Get
  • org.apache.commons.collections4.IterableGet
  • org.apache.commons.collections4.Put
  • java.util.Map

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);
   }
}

输出 (Output)

它将打印以下结果。

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 Interface

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);
   }
}

输出 (Output)

它将打印以下结果。

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_}

OrderedMap Interface

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"));  
   }
}

输出 (Output)

它将打印以下结果。

One
Two
Three

Apache Commons Collections - Ignore Null

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查非空元素

CollectionUtils的addIgnoreNull()方法可用于确保仅将非空值添加到集合中。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.addIgnoreNull()方法 -

public static <T> boolean addIgnoreNull(Collection<T> collection, T object)

参数 (Parameters)

  • collection - 要添加的集合,不能为null。

  • object - 要添加的对象,如果为null,则不会添加。

返回值 (Return Value)

如果收集更改,则为True。

异常 (Exception)

  • NullPointerException - 如果集合为null。

例子 (Example)

以下示例显示了org.apache.commons.collections4.CollectionUtils.addIgnoreNull()方法的用法。 我们正在尝试添加空值和示例非空值。

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");
      }
   }
}

输出 (Output)

它将打印以下结果。

[a]
Null value is not present

Apache Commons Collections - Merge & Sort

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

合并两个排序列表

CollectionUtils的collat​​e()方法可用于合并两个已排序的列表。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.collate()方法

public static <O extends Comparable<? super O>> List<O> 
   collate(Iterable<? extends O> a, Iterable<? extends O> b)

参数 (Parameters)

  • a - 第一个集合,不能为null。

  • b - 第二个集合,不能为空。

返回值 (Return Value)

一个新的排序列表,包含Collection a和b的元素。

异常 (Exception)

  • NullPointerException - 如果任一集合为null。

例子 (Example)

以下示例显示了org.apache.commons.collections4.CollectionUtils.collat​​e()方法的用法。 我们将合并两个排序列表,然后打印合并和排序列表。

import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class CollectionUtilsTester {
   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); 
   }
}

输出 (Output)

它将打印以下结果。

[A, B, C, D, E, F]

Commons Collections - Transforming Objects

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

转换列表

CollectionUtils的collect()方法可用于将一种类型的对象的列表转换为不同类型的对象的列表。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.collect()方法

public static <I,O> Collection<O> collect(Iterable<I> inputCollection, 
   Transformer<? super I,? extends O> transformer)

参数 (Parameters)

  • inputCollection - 从中获取输入的集合,可能不为null。

  • Transformer - 要使用的变换器,可以为null。

返回值 (Return Value)

转换结果(新列表)。

异常 (Exception)

  • NullPointerException - 如果输入集合为null。

例子 (Example)

以下示例显示了org.apache.commons.collections4.CollectionUtils.collect()方法的用法。 我们将通过解析String中的整数值将字符串列表转换为整数列表。

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);
   }
}

输出 (Output)

它将打印以下结果。

[1, 2, 3]

Commons Collections - Filtering Objects

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

使用filter()方法过滤列表

CollectionUtils的filter()方法可用于过滤列表以删除不满足传递的谓词提供的条件的对象。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.filter()方法 -

public static <T> boolean filter(Iterable<T> collection,
   Predicate<? super T> predicate)

参数 (Parameters)

  • collection - 要从中获取输入的集合,可能不为null。

  • predicate - 用作过滤器的谓词可以为null。

返回值 (Return Value)

如果此调用修改了集合,则为True,否则为false。

例子 (Example)

以下示例显示了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);
   }
}

输出 (Output)

它将打印以下结果。

Original List: [1, 2, 3, 4, 5, 6, 7, 8]
Filtered List (Even numbers): [2, 4, 6, 8]

使用filterInverse()方法过滤列表

CollectionUtils的filterInverse()方法可用于过滤列表以删除满足谓词传递提供的条件的对象。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.filterInverse()方法

public static <T> boolean filterInverse(Iterable<T> collection,
   Predicate<? super T> predicate)

参数 (Parameters)

  • collection - 要从中获取输入的集合,可能不为null。

  • predicate - 用作过滤器的谓词可以为null。

返回值 (Return Value)

如果此调用修改了集合,则为True,否则为false。

例子 (Example)

以下示例显示了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);
   }
}

输出 (Output)

它将打印以下结果。

Original List: [1, 2, 3, 4, 5, 6, 7, 8]
Filtered List (Odd numbers): [1, 3, 5, 7]

Commons Collections - Safe Empty Checks

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查非空列表

CollectionUtils的isNotEmpty()方法可用于检查列表是否为空而不必担心空列表。 因此,在检查列表大小之前,不需要将null检查放在任何地方。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.isNotEmpty()方法

public static boolean isNotEmpty(Collection<?> coll)

参数 (Parameters)

  • coll - 要检查的集合,可以为null。

返回值 (Return Value)

如果非null且非空,则为True。

例子 (Example)

以下示例显示了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);
   } 
}

输出 (Output)

它将打印以下结果。

Non-Empty List Check: false
Non-Empty List Check: false

检查空列表

CollectionUtils的isEmpty()方法可用于检查列表是否为空而不必担心空列表。 因此,在检查列表大小之前,不需要将null检查放在任何地方。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.isEmpty()方法 -

public static boolean isEmpty(Collection<?> coll)

参数 (Parameters)

  • coll - 要检查的集合,可以为null。

返回值 (Return Value)

如果为空或为空,则为真。

例子 (Example)

以下示例显示了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);
   } 
}

输出 (Output)

它将打印以下结果。

Empty List Check: true
Empty List Check: true

Apache Commons Collections - Inclusion

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查子列表

CollectionUtils的isSubCollection()方法可用于检查集合是否包含给定集合。

声明 (Declaration)

以下是声明

org.apache.commons.collections4.CollectionUtils.isSubCollection()方法 -

public static boolean isSubCollection(Collection<?> a,
   Collection<?> b)

参数 (Parameters)

  • a - 第一个(子)集合,不能为空。

  • b - 第二个(超级)集合,不得为null。

返回值 (Return Value)

当且仅当a是b的子集合时才为真。

例子 (Example)

以下示例显示了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));
   }
}

输出 (Output)

它将打印以下结果。

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 - Intersection

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查交叉口

CollectionUtils的intersection()方法可用于获取两个集合(交集)之间的公共对象。

声明 (Declaration)

以下是org.apache.commons.collections4.CollectionUtils.intersection()方法的声明

public static <O> Collection<O> intersection(Iterable<? extends O> a,
   Iterable<? extends O> b)

参数 (Parameters)

  • a - 第一个(子)集合,不能为空。

  • b - 第二个(超级)集合,不得为null。

返回值 (Return Value)

两个集合的交集。

例子 (Example)

以下示例显示了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));
   }
}

输出 (Output)

它将打印以下结果。

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 - Subtraction

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查替代品

可以使用CollectionUtils的subtract()方法通过从其他集合中减去一个集合的对象来获取新集合。

声明 (Declaration)

以下是org.apache.commons.collections4.CollectionUtils.subtract()方法的声明 -

public static <O> Collection<O> subtract(Iterable<? extends O> a,
   Iterable<? extends O> b)

参数 (Parameters)

  • a - 要从中减去的集合,不得为null。

  • b - 要减去的集合,不能为空。

返回值 (Return Value)

带有结果的新集合。

例子 (Example)

以下示例显示了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));
   }
}

输出 (Output)

它将打印以下结果。

List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
List 1 - List 2: [A, C]

Apache Commons Collections - Union

Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。

检查工会

CollectionUtils的union()方法可用于获取两个集合的并集。

声明 (Declaration)

以下是org.apache.commons.collections4.CollectionUtils.union()方法的声明

public static <O> Collection<O> union(Iterable<? extends O> a,
   Iterable<? extends O> b)

参数 (Parameters)

  • a - 第一个集合,不能为null。

  • b - 第二个集合,不能为空。

返回值 (Return Value)

两个集合的结合。

例子 (Example)

以下示例显示了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));
   }
}

输出 (Output)

它将打印以下结果。

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]
↑回到顶部↑
WIKI教程 @2018