Apache Commons Collections教程™
Apache Commons Collections是Apache Commons的组件,它们是从Java API派生而来的,并为Java语言提供了组件体系结构。 Commons-Collections试图通过提供新的接口,实现和实用程序来构建JDK类。 本教程涵盖了Apache Commons Collections的基本理解所需的大部分主题,并介绍了它的工作原理。
Apache Commons包应该是Java中使用最广发的工具包,很多框架都依赖于这组工具包中的一部分,它提供了我们常用的一些编程需要,但是JDK没能提供的机能,最大化的减少重复代码的编写。
面向读者
本教程专为初学者准备,帮助他们理解有关Apache Commons Collections相关的基本和高级概念。
前提条件
在开始练习此参考文献中提供的各种示例之前,我们假设您已经了解计算机程序和计算机编程语言。并假设您有一定的Java编程基础。
问题反馈
我们不能保证您在学习此Apache Commons Collections教程的过程中不会遇到任何问题。本教程中的讲解,示例和代码等只是根据作者的理解来概括写出。由于作者水平和能力有限,因此不保正所有编写的文章都准确无误。但是如果有遇到任何错误或问题,请反馈给我们,我们会及时纠正以方便后续读者阅读。
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Commons Collections简介 - Apache Commons Collections教程™
Commons Collections增强了Java集合框架。 它提供了几个功能来简化收集处理。 它提供了许多新的接口,实现和实用程序。 Commons Collections的主要特点如下 -
· Bag - Bag接口简化了每个对象具有多个副本的集合。
· BidiMap- BidiMap接口提供双向映射,可用于使用键或键使用的值来查找值。
· MapIterator - MapIterator接口为映射提供了简单和易于迭代方法。
· 转换装饰器 - 转换装饰器(Transforming Decorators)可以在集合添加到集合时改变集合的每个对象。
· 复合集合 - 复合集合用于要求统一处理多个集合的情况。
· 有序映射 - 有序映射保留元素添加的顺序。
· 有序集 - 有序集保留元素添加的顺序。
· 参考映射 - 参考映射允许在密切控制下对键/值进行垃圾收集。
· 比较器实现 - 许多比较器实现都可用。
· 迭代器实现 - 许多迭代器实现都可用。
· 适配器类 - 适配器类可用于将数组和枚举转换为集合。
· 实用程序 - 实用程序可用于测试测试或创建集合的典型集合理论属性,如联合,交集。 支持关闭。
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合开发环境 - Apache Commons Collections教程™
因为Commons Collections是java的一个组件,所以需要有Java编程语言运行环境的支持 请按照以下步骤设置环境。
JDK可以从链接下载Java免费获得。下载一个您使用操作系统的对应版本。
JDK下载地址:https://java.sun.com/javase/downloads/index_jdk5.jsp
按照说明下载Java并运行.exe以在您的机器上安装Java。 当在机器上安装了Java以后,需要设置环境变量来指向正确的安装目录。
有关Java的开发环境的安装,请参考以下地址: https://www.yiibai.com/java/java_environment_setup.html
Apache通用集合包下载
从commons-collections4-4.1-bin.zip下载最新版本的Apache Common Collections jar文件。 在编写本教程时,下载的是:commons-collections4-4.1-bin.zip并将其复制到一个目录文件夹中。使用Java IDE开发工具编写相关示例代码时,将下载的commons-collections4-4.1.jar添加到构建路径中即可。
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合Bag接口 - Apache Commons Collections教程™
新的接口被添加到支持bag。 Bag接口定义了一个集合,它可以计算一个对象出现在集合中的次数。 例如,如果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) | (冲突)将指定的对象到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) | 从Bag中移除所有给定的对象。 |
7 | boolean remove(Object object, int nCopies) | 从Bag中删除指定对象的nCopies副本。 |
8 | boolean removeAll(Collection<?> coll) | 删除给定集合中的所有元素,尊重基数。 |
9 | boolean retainAll(Collection<?> coll) | 移除不在给定集合中的所有Bag成员,尊重基数。 |
10 | int size() | 返回Bag中所有类型对象的总数。 |
11 | Set<E> uniqueSet() | 返回Bag中的一组唯一元素。 |
方法继承
该接口从以下接口继承方法 -
· java.util.Collection
Bag接口示例
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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合BidiMap接口 - Apache Commons Collections教程™
新接口被添加到支持双向映射。 使用双向映射,可以使用值查找键,并且可以使用键轻松查找值。
接口声明
以下是org.apache.commons.collections4.BidiMap <K,V>接口的声明 -
public interface BidiMap<K,V>
extends IterableMap<K,V>
以下是接口的方法列表 -
编号 | 方法 | 描述 |
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.Get
· org.apache.commons.collections4.IterableGet
· org.apache.commons.collections4.Put
· java.util.Map
BidiMap接口实例
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}
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合MapIterator接口 - Apache Commons Collections教程™
JDK Map接口很难作为迭代在EntrySet或KeySet对象上迭代。 MapIterator提供了对Map的简单迭代。下面的例子说明了这一点。
MapIterator接口示例
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_}
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache OrderedMap接口 - Apache Commons Collections教程™
OrderedMap是映射的新接口,用于保留添加元素的顺序。 LinkedMap和ListOrderedMap是两种可用的实现。 此接口支持Map的迭代器,并允许在Map中向前或向后两个方向进行迭代。 下面的例子说明了这一点。
示例代码
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
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
CollectionUtils类示例
Apache通用集合忽略Null - Apache Commons Collections教程™
Apache Commons Collections库的CollectionUtils类提供各种实用方法,用于覆盖广泛用例的常见操作。 它有助于避免编写样板代码。 这个库在jdk 8之前是非常有用的,但现在Java 8的Stream API提供了类似的功能。
检查是否为空元素
CollectionUtils的addIgnoreNull()方法可用于确保只有非空(null)值被添加到集合中。
声明
以下是org.apache.commons.collections4.CollectionUtils.addIgnoreNull()的声明 -
public static <T> boolean addIgnoreNull(Collection<T> collection, T object)
参数
· collection - 要添加到的集合,不能为null值。
· object - 要添加的对象,如果为null,则不会添加。
返回值
· 如果集合已更改,则返回为True。
示例
以下示例显示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");
}
}
}
执行上面示例代码,得到以下结果 -
[a]
Null value is not present
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合合并与排序 - 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 {
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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合转换对象 - Apache 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 - 要使用的transformer可能为null。
返回值
· 换结果(新列表)。
示例
以下示例显示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);
}
}
执行上面示例代码,得到以下结果 -
[1, 2, 3]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合过滤对象 - Apache 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 - 用作过滤器的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 - 用作过滤器的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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合安全空检查 - Apache 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()方法可用于检查列表是否为空。 因此,在检查列表大小之前,不需要将无效检查放在任何地方。
声明
以下是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
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合包函关系 - 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 - 第一个(子)集合不能为空。
· b - 第二个(超集)集合不能为空。
当且仅当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
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合相交 - 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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合差集 - Apache Commons Collections教程™
Apache Commons Collections库的CollectionUtils类提供各种实用方法,用于覆盖广泛用例的常见操作。 它有助于避免编写样板代码。 这个库在jdk 8之前是非常有用的,但现在Java 8的Stream API提供了类似的功能。
差集-一般地,记A,B是两个集合,则所有属于A且不属于B的元素构成的集合,叫做集合A减集合B(或集合A与集合B之差)。
求差集
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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
Apache通用集合联合 - 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]
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。




