概述
扩展 Java 集合框架的库,它在 Java 标准集合框架基础上增加了大量实用类和接口。
maven依赖
xml
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.5.0</version>
</dependency>
示例
- 代码逻辑执行指定次数
java
ClosureUtils.forClosure(3, o -> {
System.out.println(o);
}).accept("执行体");
常用API
新增集合类型
- 接口
| 接口 | 说明 |
|---|---|
| Bag | 允许重复元素且记录出现次数的无序集合 |
| SortedBag | 允许重复元素且记录出现次数的有序集合 |
| BidiMap | 双向映射(可通过值查找键),无序 |
| SortedBidiMap | 双向映射(可通过值查找键),有序 |
| OrderedBidiMap | 提供有序操作 |
| MultiValueMap | 一个键对应多个值的映射 |
| OrderedMap | 保持插入顺序的映射 |
| ListOrderedSet | 保持插入顺序的 Set |
- 可用实现类
| 实现类 | 接口 | 唯一性支持 | 排序支持 | 使用场景 | 备注 |
|---|---|---|---|---|---|
| HashBag | Bag | 允许多个相同元素 | 无序集合 | 用于计数功能且无序的场景 | 实现类 |
| CollectionBag | Bag | 允许多个相同元素 | 无序集合 | 用于与标准集合 API 交互 | 包装器类 |
| PredicatedBag | Bag | 允许多个相同元素 | 无序集合 | 需要确保集合元素符合特定条件 | 包装器类 |
| SynchronizedBag | Bag | 允许多个相同元素 | 无序集合 | 多线程环境下的集合操作 | 包装器类 |
| TransformedBag | Bag | 允许多个相同元素 | 无序集合 | 需要在添加/获取时转换元素类型 | 包装器类 |
| UnmodifiableBag | Bag | 允许多个相同元素 | 无序集合 | 需要防止集合被意外修改 | 包装器类 |
| TreeBag | SortedBag | 允许多个相同元素 | 有序集合 | 用于计数功能且有序的场景 | 实现类 |
| CollectionSortedBag | SortedBag | 允许多个相同元素 | 有序集合 | 用于与标准集合 API 交互 | 包装器类 |
| PredicatedSortedBag | SortedBag | 允许多个相同元素 | 有序集合 | 用于集合元素符合特定条件 | 包装器类 |
| SynchronizedSortedBag | SortedBag | 允许多个相同元素 | 有序集合 | 多线程环境下的集合操作 | 包装器类 |
| TransformedSortedBag | SortedBag | 允许多个相同元素 | 有序集合 | 用于添加/获取时转换元素类型 | 包装器类 |
| UnmodifiableSortedBag | SortedBag | 允许多个相同元素 | 有序集合 | 用于防止集合被意外修改 | 包装器类 |
| DualHashBidiMap | BidiMap | 键、值唯一 | 无序 | 快速查找的双向映射 | |
| DualLinkedHashBidiMap | BidiMap | 键、值唯一 | 无序 | 维护插入顺序的双向映射 | |
| UnmodifiableBidiMap | BidiMap | 键、值唯一 | 无序 | 创建不可修改的双向映射视图 | 包装器类 |
| TreeBidiMap | OrderedBidiMap | 键、值唯一 | 有序 | 需要排序的双向映射 | |
| UnmodifiableOrderedBidiMap | OrderedBidiMap | 键、值唯一 | 有序 | 不可修改有序双向映射 | 包装器类 |
| DualTreeBidiMap | SortedBidiMap | 键、值唯一 | 有序 | 需要键有序且支持范围查询 | |
| UnmodifiableSortedBidiMap | SortedBidiMap | 键、值唯一 | 有序 | 创建不可修改的有序双向映射视图 | 包装器类 |
| ArrayListValuedHashMap | ListValuedMap | 键唯一、值多值不唯一 | 键无序 | 需要快速查找的多值映射 | |
| ArrayListValuedLinkedHashMap | ListValuedMap | 键唯一、值多值不唯一 | 键有序 | 需要维护键插入顺序的多值映射 | |
| HashSetValuedHashMap | SetValuedMap | 键唯一、值多值唯一 | 键无序 | ||
| LinkedHashSetValuedLinkedHashMap | SetValuedMap | 键唯一、值多值唯一 | 键有序 | ||
| TransformedMultiValuedMap | MultiValuedMap | 键唯一、值不唯一 | 用于键值转换 | 包装器类 | |
| UnmodifiableMultiValuedMap | MultiValuedMap | 键唯一、值不唯一 | 用于不可修改 | 包装器类 |
常用工具类
- BagUtils
| 方法 | 说明 |
|---|---|
| emptyBag() | 获取空的Bag集合 |
| emptySortedBag() | 获取空的SortedBag集合 |
| collectionBag(Bag<E> bag) | 获取 Collection交互包装类 |
| predicatedBag(Bag<E> bag, Predicate<? super E> predicate) | 获取符合特定条件元素的包装类 |
| predicatedSortedBag(SortedBag bag, Predicate<? super E> predicate) | 获取符合特定条件元素的包装类 |
| synchronizedBag(Bag<E> bag) | 获取线程安全的包装类 |
| synchronizedSortedBag(SortedBag<E> bag) | 获取线程安全的包装类 |
| transformingBag(Bag<E> bag, Transformer<? super E, ? extends E> transformer) | 获取类型转换的包装类 |
| transformingSortedBag(SortedBag<E> bag, Transformer<? super E, ? extends E> transformer) | 获取类型转换的包装类 |
| unmodifiableBag(Bag<? extends E> bag) | 获取不可修改的包装类 |
| unmodifiableSortedBag(SortedBag<E> bag) | 获取不可修改的包装类 |
ClosureUtils
| 方法 | 说明 |
|---|---|
| nopClosure() | 返回不执行任何操作的闭包 |
| exceptionClosure() | 返回总是抛出异常的闭包 |
| forClosure(int count, Closure<? super E> closure) | 重复执行闭包指定次数 |
| switchClosure(Map<Predicate<E>, Closure<E>> predicatesAndClosures) switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures) switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) | 条件满足时重复执行 |
| whileClosure(Predicate<? super E> predicate, Closure<? super E> closure) | 根据条件循环执行 |
| doWhileClosure(Closure<? super E> closure, Predicate<? super E> predicate) | 先执行一次,再条件循环 |
| ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure) ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure) | 根据条件选择执行 |
| switchMapClosure(Map<? extends E, Closure<E>> objectsAndClosures) | 根据输入对象选择闭包 |
| asClosure(Transformer<? super E, ?> transformer) | 将 Transformer 接口实现包装为 Closure |
| chainedClosure(Closure<? super E>... closures) chainedClosure(Collection<? extends Closure<? super E>> closures) | 将多个闭包串联执行 |
| invokerClosure(String methodName) invokerClosure(String methodName, Class<?>[] paramTypes, Object[] args) | 通过反射调用对象的方法 |
CollectionUtils
| 方法 | 说明 |
|---|---|
| emptyCollection() | 获取一个空集合 |
| emptyIfNull(Collection<T> collection) | 如果集合为null则返回一个空集合 |
| addAll(Collection<C> collection, C... elements) addAll(Collection<C> collection, Enumeration<?> enumeration) addAll(Collection<C> collection, Iterable<?> iterable) addAll(Collection<C> collection, Iterator<?> iterator) | 添加元素 |
| addIgnoreNull(Collection<T> collection, T object) | 添加元素,忽略null值。 |
| collate(Iterable<?> a, Iterable<?> b) collate(Iterable<?> a, Iterable<?> b, boolean includeDuplicates) collate(Iterable<?> a, Iterable<?> b, Comparator<? super O> c) collate(Iterable<?> iterableA, Iterable<?> iterableB, Comparator<?> comparator, boolean includeDuplicates) | 合并两个已排序的列表。 |
| collect(Iterable<?> inputCollection, Transformer<? super I, ?> transformer, R outputCollection) collect(IterableinputCollection, Transformer<? super I, ?> transformer) collect(Iterator<?> inputIterator, Transformer<? super I, ?> transformer, R outputCollection) collect(IteratorinputIterator, Transformer<? super I, ?> transformer)** | 将一种类型的对象列表转换为另一种类型的对象列表。 |
| containsAll(Collection<?> coll1, Collection<?> coll2) | 用于判断一个集合是否包含另一个集合中的所有元素。 |
| containsAny(Collection<?> coll1, Collection<?> coll2) containsAny(Collection<?> coll1, T... coll2) | 用于判断两个集合是否存在至少一个共同元素。 |
| disjunction(Iterable<?> a, Iterable<?> b) | 用于获取两个集合的对称差集(即不属于交集的元素)。 |
| subtract(Iterable<?> a, Iterable<?> b) subtract(Iterable<?> a, Iterable<?> b, Predicate<O> p) | 用于获取两个集合的差集 |
| intersection(Iterable<?> a, Iterable<?> b) | 用于获取两个集合的交集 |
| union(Iterable<?> a, Iterable<?> b) | 用于获取两个集合的并集 |
| extractSingleton(Collection<E> collection) | 用于从仅包含单个元素的集合中提取该元素。如果集合为空或包含多个元素,则会抛出异常。 |
| filter(Iterable<T> collection, Predicate<? super T> predicate) | 元素过滤,保留符合条件的元素 |
| filterInverse(Iterable<T> collection, Predicate<? super T> predicate) | 元素过滤,移除符合条件的元素 |
| get(Map<K, V> map, int index) get(Object object, int index) | 获取指定索引的元素 |
| getCardinalityMap(Iterable<?> coll) | 用于统计集合中每个元素的出现次数 |
| isEmpty(Collection<?> coll) | 判断集合是否为空 |
| isEqualCollection(Collection<?> a, Collection<?> b) isEqualCollection(Collection<?> a, Collection<?> b, Equator<? super E> equator) | 用于比较两个集合的元素是否完全相同(不考虑顺序)。 |
| isFull(Collection<? extends Object> collection) | 判断集合是否为null |
| isNotEmpty(Collection<?> coll) | 判断集合是否不为空 |
| isProperSubCollection(Collection<?> a, Collection<?> b) | 用于验证一个集合是否是另一个集合的真子集 |
| isSubCollection(Collection<?> a, Collection<?> b) | 用于验证一个集合的所有元素是否都存在于另一个集合中。 |
| maxSize(Collection<? extends Object> collection) | 用于获取固定大小集合的最大容量。 |
| permutations(Collection<E> collection) | 生成一个集合中所有元素的所有可能排列顺序 |
| predicatedCollection(Collection<C> collection, Predicate<? super C> predicate) | 用于创建一个基于Predicate过滤的集合 |
| removeAll(Collection<E> collection, Collection<?> remove) removeAll(Iterable<E> collection, Iterable<?> remove, Equator<? super E> equator) | 用于从目标集合中移除所有与指定集合中匹配的元素。 |
| removeCount(Collection<E> input, int startIndex, int count) | 移除指定索引位置的指定数量的元素 |
| removeRange(Collection<E> input, int startIndex, int endIndex) | 移除指定索引范围的元素 |
| retainAll(Collection<C> collection, Collection<?> retain) retainAll(Iterable<E> collection, Iterable<?> retain, Equator<? super E> equator) | 用于获取两个集合的交集(保留目标集合中同时存在于指定集合的元素)。 |
| reverseArray(Object[] array) | 用于反转数组中元素的顺序 |
| select(Iterable<?> inputCollection, Predicate<? super O> predicate) select(Iterable<?> inputCollection, Predicate<? super O> predicate, R outputCollection) select(Iterable<?> inputCollection, Predicate<? super O> predicate, R outputCollection, R rejectedCollection) | 用于根据条件筛选集合元素 |
| selectRejected(Iterable<?> inputCollection, Predicate<? super O> predicate) selectRejected(Iterable<?> inputCollection, Predicate<? super O> predicate, R outputCollection) | 用于获取不满足指定条件的元素集合。 |
| size(Object object) | 获取集合元素数量 |
| sizeIsEmpty(Object object) | 判断集合是否为null或者元素数量为0 |
| transform(Collection<C> collection, Transformer<? super C, ?> transformer) | 用于将集合中的每个元素通过指定函数进行转换。 |
| transformingCollection(Collection<E> collection, Transformer<? super E, ?> transformer) | 用于在元素添加/修改时自动应用转换逻辑。 |
ComparatorUtils
| 方法 | 说明 |
|---|---|
| booleanComparator(boolean trueFirst) | 用于创建比较 Boolean 对象的比较器。 |
| chainedComparator(Collection<Comparator<E>> comparators) chainedComparator(Comparator<E>... comparators) | 用于将多个比较器串联组合,按顺序依次应用,实现多条件排序 |
| max(E o1, E o2, Comparator<E> comparator) | 最大值 |
| min(E o1, E o2, Comparator<E> comparator) | 最小值 |
| naturalComparator() | 用于获取基于对象自然顺序的比较器 |
| nullHighComparator(Comparator<E> comparator) | 获取null 排在最后的比较器 |
| nullLowComparator(Comparator<E> comparator) | 获取null 排在前面的比较器 |
| reversedComparator(Comparator<E> comparator) | 用于反转现有比较器的排序顺序。 |
| transformedComparator(Comparator<O> comparator, Transformer<? super I, ?> transformer) | 用于通过转换函数预处理比较对象后再进行排序。 |
EnumerationUtils
| 方法 | 说明 |
|---|---|
| asIterable(Enumeration<T> enumeration) | 用于将 Enumeration 转换为可单次遍历的 Iterable 对象。 |
| get(Enumeration<T> e, int index) | 获取指定索引的枚举项 |
| toList(Enumeration<? extends E> enumeration) toList(StringTokenizer stringTokenizer) | 用于将 Enumeration 转换为可 List 对象。 |
| toSet(Enumeration<? extends E> enumeration) | 用于将 Enumeration 转换为可 Set 对象。 |
FactoryUtils
| 方法 | 说明 |
|---|---|
| constantFactory(T constantToReturn) | 用于创建始终返回同一对象的工厂实例 |
| exceptionFactory() | 用于创建始终抛出指定异常的工厂实例 |
| instantiateFactory(Class<T> classToInstantiate) instantiateFactory(Class classToInstantiate, Class<?>[] paramTypes, Object[] args) | 用于创建通过无参构造函数实例化对象的工厂。 |
| nullFactory() | 用于创建始终返回 null 的工厂实例 |
| prototypeFactory(T prototype) | 用于创建通过克隆原型对象生成新实例的工厂。 |
IterableUtils
| 方法 | 说明 |
|---|---|
| boundedIterable(Iterable<E> iterable, final long maxSize) | 用于返回一个包含指定数量元素的迭代器 |
| chainedIterable(final Iterable<?>... iterables) chainedIterable(Iterable<?> a, Iterable<?> b) chainedIterable(Iterable<?> a, Iterable<?> b, Iterable<?> c) chainedIterable(Iterable<?> a, Iterable<?> b, Iterable<?> c, Iterable<?> d) | 用于将多个可迭代对象连接成一个单一的惰性迭代器。 |
| collatedIterable(Comparator<? super E> comparator, Iterable<?> a, final Iterable<?> b) collatedIterable(Iterable<?> a, Iterable<?> b) | 用于将两个可迭代对象合并为一个有序的迭代器,支持自然排序或自定义比较器排序。 |
| contains(Iterable<?> iterable, E object, Equator<? super E> equator) contains(Iterable<E> iterable, Object object) | 用于检查可迭代对象中是否包含指定元素,支持自定义比较逻辑。 |
| countMatches(Iterable<E> input, Predicate<? super E> predicate) | 用于统计可迭代对象中满足指定条件的元素数量。 |
| duplicateList(Iterable<E> iterable) | 用于返回可迭代对象中所有重复元素的列表(按首次出现顺序保留重复项)。 |
| duplicateSequencedSet(Iterable<E> iterable) | 用于返回可迭代对象中所有重复元素的有序集合,保留元素首次出现的顺序。 |
| duplicateSet(Iterable<E> iterable) | 用于返回可迭代对象中所有重复元素的无序集合,不保留原始顺序。 |
| emptyIfNull(Iterable<E> iterable) | 用于安全处理可能为 null 的可迭代对象(Iterable),若输入为 null 则返回一个不可变的空集合。 |
| emptyIterable() | 用于返回一个不可变的空迭代器(Iterable),适用于需要显式返回空集合而非 null 的场景。 |
| filteredIterable(Iterable<E> iterable, Predicate<? super E> predicate) | 用于返回一个过滤后的迭代器视图,仅包含满足指定条件的元素。 |
| find(Iterable<E> iterable, Predicate<? super E> predicate) | 用于从数组或集合中查找并返回第一个满足指定条件的元素。 |
| first(Iterable<T> iterable) | 用于从可迭代对象(Iterable)中返回第一个元素,若集合为空或为 null 则返回 null。 |
| forEach(Iterable<E> iterable, Closure<? super E> closure) | 遍历集合中的所有元素,对每个元素执行指定操作 |
| forEachButLast(Iterable<E> iterable, Closure<? super E> closure) | 遍历集合中的所有元素,跳过最后一个元素,对剩余元素执行指定操作 |
| frequency(Iterable<E> iterable, T obj) | 用于统计可迭代对象中指定元素的出现次数。 |
| get(Iterable<T> iterable, int index) | 获取指定索引的元素 |
| indexOf(Iterable<E> iterable, Predicate<? super E> predicate) | 查找元素索引 |
| isEmpty(Iterable<?> iterable) | 判断集合是否为空 |
| loopingIterable(final Iterable<E> iterable) | 用于创建一个无限循环遍历输入集合的可迭代对象。 |
| matchesAll(Iterable<E> iterable, Predicate<? super E> predicate) | 判断集合中所有元素是否满足指定条件 |
| matchesAny(Iterable<E> iterable, Predicate<? super E> predicate) | 判断集合中任意元素是否满足指定条件 |
| partition(Iterable<? extends O> iterable, Factory<R> partitionFactory, Predicate<? super O>... predicates) partition(Iterable<? extends O> iterable, Predicate<? super O> predicate) partition(Iterable<? extends O> iterable, Predicate<? super O>... predicates) | 用于将可迭代对象分割为满足和不满足指定条件的两个子集合。 |
| reversedIterable(Iterable<E> iterable) | 用于创建一个逆序遍历输入集合的可迭代对象。 |
| size(Iterable<?> iterable) | 获取元素数量 |
| skippingIterable(Iterable<E> iterable, long elementsToSkip) | 用于创建一个跳过指定数量元素的可迭代对象。 |
| toList(Iterable<E> iterable) | 用于将任意 Iterable 对象转换为 List |
| toString(Iterable<E> iterable) toString(Iterable<E> iterable, Transformer<? super E, String> transformer) toString(Iterable<E> iterable, Transformer<? super E, String> transformer, String delimiter, String prefix, String suffix) | 用于将任意 Iterable 对象转换为字符串 |
| transformedIterable(Iterableiterable, Transformer<? super I, ? extends O> transformer) | 用于创建一个对原始集合元素进行转换后的新可迭代对象。 |
| uniqueIterable(Iterable<E> iterable) | 用于创建去重后的可迭代对象 |
| unmodifiableIterable(Iterable<E> iterable) | 用于创建不可修改的可迭代对象 |
| zippingIterable(Iterable<?> a, Iterable<?> b) zippingIterable(final Iterable<?> first, final Iterable<?>... others) | 用于将多个可迭代对象(Iterables)的元素按顺序交替合并为一个新的可迭代对象 |
IteratorUtils
| 方法 | 说明 |
|---|---|
| emptyIterator() | 返回一个空的 Iterator,调用 hasNext() 始终返回 false |
| emptyListIterator() | 返回一个空的 ListIterator(支持双向遍历),但无任何元素。 |
| emptyMapIterator() | 返回一个空的 MapIterator(用于遍历 Map 的键值对),但无任何条目。 |
| emptyOrderedIterator() | 返回一个空的 OrderedIterator(支持有序遍历的迭代器),但无元素。 |
| emptyOrderedMapIterator() | 返回一个空的 OrderedMapIterator(支持有序遍历 Map 的键值对),但无条目。 |
| arrayIterator(E... array) arrayIterator(E[] array, int start) arrayIterator(E[] array, int start, int end) arrayIterator(Object array) arrayIterator(Object array, int start) arrayIterator(Object array, int start, int end) | 用于将数组转换为可重置的迭代器 |
| arrayListIterator(E... array) arrayListIterator(E[] array, int start) arrayListIterator(E[] array, int start, int end) arrayListIterator(Object array) arrayListIterator(Object array, int start) arrayListIterator(Object array, int start, int end) | 用于将数组转换为可重置的列表迭代器,支持双向遍历和修改操作 |
| asEnumeration(Iterator<?> iterator) | 将 Iterator 转换为 Enumeration |
| asIterable(Iterator<?> iterator) | 用于将 Iterator 转换为 Iterable |
| asIterator(Enumeration<?> enumeration) asIterator(Enumeration<?> enumeration, Collection<? super E> removeCollection) | 将 Enumeration 反向转换为 Iterator,以便使用现代 Java 集合操作。 |
| asMultipleUseIterable(Iterator<?> iterator) | 用于将单次使用的 Iterator 转换为可多次遍历的 Iterable,通过缓存迭代元素实现重复访问 |
| boundedIterator(Iterator<?> iterator, long max) boundedIterator(Iterator<?> iterator, long offset, long max) | 返回一个迭代器,限制原始迭代器的遍历范围 |
| chainedIterator(Collection<? extends Iterator<?>> iterators) chainedIterator(Iterator<?>... iterators) chainedIterator(Iterator<?> iterator1, Iterator<?> iterator2) chainedIterator(final Iterator<? extends Iterator<?>> iterators) | 将多个迭代器串联成一个逻辑上连续的迭代器,按顺序遍历所有元素。 |
| collatedIterator(Comparator<? super E> comparator, Collection |
ListUtils
| 方法 | 说明 |
|---|---|
| defaultIfNull(List<T> list, List<T> defaultList) | 如果为null设置默认值 |
| emptyIfNull(List<T> list) | 如果为null返回空集合 |
| fixedSizeList(List<E> list) | 用于创建一个固定大小的list集合,禁止通过该视图添加或删除元素,但允许修改现有元素。 |
| getFirst(List<T> list) | 获取第一个元素 |
| getLast(List<T> list) | 获取最后一个元素 |
| hashCodeForList(Collection<?> list) | 用于计算列表的哈希值 |
| indexOf(List<E> list, Predicate<E> predicate) | 获取元素索引 |
| intersection(List<?> list1, List<?> list2) | 用于获取两个集合的交集 |
| isEqualList(Collection<?> list1, Collection<?> list2) | 用于严格比较两个列表的元素顺序和内容是否完全一致。 |
| lazyList(List<E> list, Factory<?> factory) lazyList(List<E> list, Transformer<Integer, ?> transformer) | 用于创建懒加载列表,其元素在首次访问时通过工厂方法动态生成。 |
| longestCommonSubsequence(CharSequence charSequenceA, CharSequence charSequenceB) longestCommonSubsequence(List<E> a, List<E> b) longestCommonSubsequence(List<E> listA, List<E> listB, Equator<? super E> equator) | 用于计算两个序列(字符串或列表)的最长公共子序列(LCS),返回包含公共元素的列表。 |
| partition(List<T> list, int size) | 用于按指定数量将集合的两个子集合。 |
| predicatedList(List<E> list, Predicate< predicate) | |
| removeAll(Collection<E> collection, Collection<?> remove) | 用于从目标集合中移除所有与指定集合中匹配的元素。 |
| retainAll(Collection<E> collection, Collection<?> retain) | 用于获取两个集合的交集(保留目标集合中同时存在于指定集合的元素)。 |
| select(Collection<?> inputCollection, Predicate<? super E> predicate) | 用于根据条件筛选集合元素 |
| selectRejected(Collection<?> inputCollection, Predicate<? super E> predicate) | 用于获取不满足指定条件的元素集合。 |
| subtract(List<E> list1, List<?> list2) | 用于获取两个集合的差集 |
| sum(List<?> list1, List<?> list2) | 用于合并两个列表并自动去重,返回包含所有唯一元素的新列表 |
| union(List<?> list1, List<?> list2) | 用于合并两个列表并保留所有元素(包括重复项) |
| synchronizedList(List<E> list) | 用于创建线程安全列表 |
| transformedList(List<E> list, Transformer<? super E, ?> transformer) | 用于创建对原列表元素的实时转换集合,每次访问都会动态应用转换函数。 |
| unmodifiableList(List<?> list) | 用于创建不可修改集合 |
MapUtils
| 方法 | 说明 |
|---|---|
| debugPrint(PrintStream out, Object label, Map<?, ?> map) | 用于以易读的格式打印 Map 内容 |
| emptyIfNull(Map<K, V> map) | 当集合为 null 时返回空集合 |
| fixedSizeMap(Map<K, V> map) | 用于将普通 Map 包装为固定大小的不可变 Map |
| fixedSizeSortedMap(SortedMap<K, V> map) | 用于将普通 SortedMap 包装为固定大小的不可变 SortedMap |
| getBoolean(Map<?, ?> map, K key) getBoolean(Map<?, ?> map, K key, Boolean defaultValue) getBoolean(Map<?, ?> map, K key, Function<K, Boolean> defaultFunction) | 用于从 Map 中安全获取 Boolean 类型值 |
| getBooleanValue(Map<?, ?> map, K key) getBooleanValue(Map<?, ?> map, K key, boolean defaultValue) getBooleanValue(Map<?, ?> map, K key, Function<K, Boolean> defaultFunction) | 用于从 Map 中安全获取 boolean 基本类型值 |
| getByte(Map<?, ?> map, K key) getByte(Map<?, ?> map, K key, Byte defaultValue) getByte(Map<?, ?> map, K key, Function<K, Byte> defaultFunction) | 用于从 Map 中安全获取 Byte 类型值 |
| getByteValue(Map<?, ?> map, K key) getByteValue(Map<?, ?> map, K key, byte defaultValue) getByteValue(Map<?, ?> map, K key, Function<K, Byte> defaultFunction) | 用于从 Map 中安全获取 byte 基本类型值 |
| getDouble(Map<?, ?> map, K key) getDouble(Map<?, ?> map, K key, Double defaultValue) getDouble(Map<?, ?> map, K key, Function<K, Double> defaultFunction) | 用于从 Map 中安全获取Double 类型值 |
| getDoubleValue(Map<?, ?> map, K key) getDoubleValue(Map<?, ?> map, K key, double defaultValue) getDoubleValue(Map<?, ?> map, K key, Function<K, Double> defaultFunction) | 用于从 Map 中安全获取double 基本类型值 |
| getFloat(Map<?, ?> map, K key) getFloat(Map<?, ?> map, K key, Float defaultValue) getFloat(Map<?, ?> map, K key, Function<K, Float> defaultFunction) | 用于从 Map 中安全获取Float 类型值 |
| getFloatValue(Map<?, ?> map, K key) getFloatValue(Map<?, ?> map, K key, float defaultValue) getFloatValue(Map<?, ?> map, K key, Function<K, Float> defaultFunction) | 用于从 Map 中安全获取 float 基本类型值 |
| getInteger(Map<?, ?> map, K key) getInteger(Map<?, ?> map, K key, Function<K, Integer> defaultFunction) getInteger(Map<?, ?> map, K key, Integer defaultValue) | 用于从 Map 中安全获取 Integer 类型值 |
| getIntValue(Map<?, ?> map, K key) getIntValue(Map<?, ?> map, K key, Function<K, Integer> defaultFunction) getIntValue(Map<?, ?> map, K key, int defaultValue) | 用于从 Map 中安全获取 int 基本类型值 |
| getLong(Map<?, ?> map, K key) getLong(Map<?, ?> map, K key, Function<K, Long> defaultFunction) getLong(Map<?, ?> map, K key, Long defaultValue) | 用于从 Map 中安全获取 Long 类型值 |
| getLongValue(Map<?, ?> map, K key) getLongValue(Map<?, ?> map, K key, Function<K, Long> defaultFunction) getLongValue(Map<?, ?> map, K key, long defaultValue) | 用于从 Map 中安全获取 long 基本类型值 |
| getMap(Map<?, ?> map, K key) getMap(Map<?, ?> map, K key, Function<K, Map<?, ?>> defaultFunction) getMap(Map<?, ?> map, K key, Map<?, ?> defaultValue) | 用于从 Map 中安全获取嵌套的 Map 类型值 |
| getNumber(Map<?, ?> map, K key) getNumber(Map<?, ?> map, K key, Function<K, Number> defaultFunction) getNumber(Map<?, ?> map, K key, Number defaultValue) | 用于从 Map 中安全获取 Number 类型值 |
| getObject(Map<?, V> map, K key) getObject(Map<K, V> map, K key, V defaultValue) | 用于从 Map 中安全获取 Object类型值 |
| getShort(Map<?, ?> map, K key) getShort(Map<?, ?> map, K key, Function<K, Short> defaultFunction) getShort(Map<?, ?> map, K key, Short defaultValue) | 用于从 Map 中安全获取 Short 类型值 |
| getShortValue(Map<?, ?> map, K key) getShortValue(Map<?, ?> map, K key, Function<K, Short> defaultFunction) getShortValue(Map<?, ?> map, K key, short defaultValue) | 用于从 Map 中安全获取 short 基本类型值 |
| getString(Map<?, ?> map, K key) getString(Map<?, ?> map, K key, Function<K, String> defaultFunction) getString(Map<?, ?> map, K key, String defaultValue) | 用于从 Map 中安全获取 String类型值 |
| invertMap(Map<K, V> map) | 用于反转 Map 的键值对,返回一个新的 Map 对象 |
| isEmpty(Map<?, ?> map) | 判断集合是否为空 |
| isNotEmpty(Map<?, ?> map) | 判断集合是否不为空 |
| iterableMap(Map<K, V> map) | 用于将普通 Map 转换为支持迭代器操作的 IterableMap 对象,从而简化 Map 的遍历和删除操作 |
| iterableSortedMap(SortedMap<K, V> sortedMap) | 用于将普通 SortedMap 转换为支持迭代器操作的 IterableSortedMap 对象,从而简化有序 Map 的遍历和删除操作 |
| lazyMap(Map<K, V> map, Factory<?> factory) lazyMap(Map<K, V> map, Transformer<?, ?> transformerFactory) | 提供的懒加载 Map 实现,可在访问键时自动创建值对象,避免手动初始化 |
| lazySortedMap(SortedMap<K, V> map, Factory<?> factory) lazySortedMap(SortedMap<K, V> map, Transformer<?, ?> transformerFactory) | 提供的懒加载有序 Map 实现,继承自 SortedMap 接口,在访问键时自动创建值对象,同时保持键的有序性 |
| orderedMap(Map<K, V> map) | 用于支持有序的键值对映射,允许通过键的顺序进行前向/反向迭代 |
| populateMap(Map<K, V> map, Iterable<?> elements, Transformer<E, K> keyTransformer, Transformer<E, V> valueTransformer) populateMap(Map<K, V> map, Iterable<?> elements, Transformer<V, K> keyTransformer) populateMap(MultiMap<K, V> map, Iterable<?> elements, Transformer<E, K> keyTransformer, Transformer<E, V> valueTransformer) populateMap(MultiMap<K, V> map, Iterable<?> elements, Transformer<V, K> keyTransformer) | 用于批量填充 Map 数据,支持自定义键值生成规则,可显著简化 Map 初始化代码 |
| predicatedMap(Map<K, V> map, Predicate<?> keyPred, Predicate<? super V> valuePred) | 用于对 Map 的键和值进行动态校验,确保所有操作符合预设条件 |
| predicatedSortedMap(SortedMap<K, V> map, Predicate<?> keyPred, Predicate<? super V> valuePred) | 用于对 SortedMap 的键和值进行动态校验,确保所有操作符合预设条件,同时保持键的有序性 |
| putAll(Map<K, V> map, Object[] array) | 用于将二维数组或键值对集合批量填充到目标 Map 中,支持类型安全的键值转换 |
| safeAddToMap(Map<?, Object> map, K key, Object value) | 用于安全地向 Map 中添加键值对,避免因 null 值或重复键导致的异常 |
| size(Map<?, ?> map) | 获取map集合大小 |
| synchronizedMap(Map<K, V> map) | 用于将普通 Map 包装为线程安全的同步 Map |
| synchronizedSortedMap(SortedMap<K, V> map) | 用于将普通 SortedMap 包装为线程安全的同步 SortedMap |
| toMap(ResourceBundle resourceBundle) | 用于将 ResourceBundle 转换为 Map<String, Object> 结构 |
| toProperties(Map<K, V> map) | 用于将 Map<String, String> 转换为 java.util.Properties 对象 |
| transformedMap(Map<K, V> map, Transformer<?, ?> keyTransformer, Transformer<? super V, ?> valueTransformer) | 用于返回一个基于原始 Map 的转换,其中键和值会经过指定的转换器处理。 |
| transformedSortedMap(SortedMap<K, V> map, Transformer<?, ?> keyTransformer, Transformer<? super V, ?> valueTransformer) | 用于返回一个基于原始 SortedMap 的转换,其中键和值会经过指定的转换器处理 |
| unmodifiableMap(Map<?, ?> map) | 用于返回一个不可修改的 Map 实例 |
| unmodifiableSortedMap(SortedMap<K, ?> map) | 用于返回一个不可修改的 SortedMap实例 |
| verbosePrint(PrintStream out, Object label, Map<?, ?> map) | 用于以格式化的方式打印 Map 内容(包含键值对的换行显示) |
MultiMapUtils
| 方法 | 说明 |
|---|---|
| emptyIfNull(MultiValuedMap<K, V> map) | 用于安全处理可能为 null 的 MultiValuedMap |
| emptyMultiValuedMap() | 用于返回一个不可变的、类型安全的空 MultiValuedMap 实例 |
| getCollection(MultiValuedMap<K, V> map, K key) | 用于以 null-safe 的方式从 MultiValuedMap 中获取指定键对应的集合 |
| getValuesAsBag(MultiValuedMap<K, V> map, K key) | 用于以 null-safe 的方式从 MultiValuedMap 中获取指定键对应的 Bag |
| getValuesAsList(MultiValuedMap<K, V> map, K key) | 用于以 null-safe 的方式从 MultiValuedMap 中获取指定键对应的 List |
| getValuesAsSet(MultiValuedMap<K, V> map, K key) | 用于以 null-safe 的方式从 MultiValuedMap 中获取指定键对应的 Set |
| isEmpty(MultiValuedMap<?, ?> map) | 判断是否为空 |
| newListValuedHashMap() | 用于创建一个以 ArrayList 作为值集合的 ListValuedMap 实例 |
| newSetValuedHashMap() | 用于创建一个以 HashSet 作为值集合的 SetValuedMap 实例 |
| transformedMultiValuedMap(MultiValuedMap<K, V> map, Transformer<?, ?> keyTransformer, Transformer<? super V, ? extends V> valueTransformer) | 用于在向 MultiValuedMap 添加键值对时自动转换键或值的类型 |
| unmodifiableMultiValuedMap(MultiValuedMap<?, ? extends V> map) | 用于包装任意 MultiValuedMap 实例,防止外部修改其内容。 |
MultiSetUtils
| 方法 | 说明 |
|---|---|
| emptyMultiSet() | 用于返回一个空 MultiSet 实例 |
| predicatedMultiSet(MultiSet<E> multiset, Predicate<? super E> predicate) | 用于在向 MultiSet 添加元素时通过谓词(Predicate)进行条件校验,只有满足条件的元素才会被实际添加 |
| synchronizedMultiSet(MultiSet<E> multiset) | 用于包装任意 MultiSet 实例,使其所有操作在同步块中执行 |
| unmodifiableMultiSet(MultiSet<? extends E> multiset) | 用于包装任意 MultiSet 实例,防止外部修改其内容。 |
PredicateUtils
| 方法 | 说明 |
|---|---|
| allPredicate(Collection<? extends Predicate<?>> predicates) allPredicate(Predicate<?>... predicates) | 用于组合多个 Predicate 条件,生成一个逻辑"与"(AND)的复合谓词 |
| andPredicate(Predicate<?> predicate1, Predicate<?> predicate2) | 用于组合两个 Predicate 条件,生成一个逻辑"与"(AND)的复合谓词 |
| anyPredicate(Collection<? extends Predicate<?>> predicates) anyPredicate(Predicate<?>... predicates) | 用于组合多个 Predicate 条件,生成一个逻辑"或"(OR)的复合谓词 |
| asPredicate(Transformer<?, Boolean> transformer) | 用于将 Transformer 对象转换为 Predicate 对象 |
| eitherPredicate(Predicate<?> predicate1, Predicate<?> predicate2) | 用于组合两个 Predicate 条件,生成一个逻辑"异或"(XOR)的复合谓词 |
| neitherPredicate(Predicate<?> predicate1, Predicate<?> predicate2) | 用于创建一个逻辑"非与"(NOR)的组合谓词 |
| equalPredicate(T value) | 用于创建一个基于对象相等性(equals())的谓词条件 |
| exceptionPredicate() | 用于创建一个始终抛出异常的 Predicate 对象 |
| falsePredicate() | 用于创建一个始终返回 false 的 Predicate 对象 |
| truePredicate() | 用于创建一个始终返回 true的 Predicate 对象 |
| identityPredicate(T value) | 用于创建一个基于对象标识(==)的谓词条件 |
| instanceofPredicate(Class<?> type) | 用于创建一个基于类型检查(instanceof)的谓词条件 |
| invokerPredicate(String methodName) invokerPredicate(String methodName, Class<?>[] paramTypes, Object[] args) | 用于通过反射调用对象的指定方法并返回结果的谓词条件 |
| nonePredicate(Collection<? extends Predicate<?>> predicates) nonePredicate(Predicate<?>... predicates) | 用于创建一个逻辑"全否"(NOR)的组合谓词,即当所有子谓词均不满足时返回 true |
| notNullPredicate() | 用于创建非空检查的谓词条件 |
| notPredicate(Predicate<?> predicate) | 用于创建一个逻辑"非"的谓词,即对原谓词的判断结果取反 |
| nullIsExceptionPredicate(Predicate<?> predicate) | 用于创建一个当输入为 null 时抛出异常的谓词 |
| nullIsFalsePredicate(Predicate<?> predicate) | 用于创建一个当输入为 null 时直接返回 false 的谓词 |
| nullIsTruePredicate(Predicate<?> predicate) | 用于创建一个当输入为 null 时直接返回 true 的谓词 |
| nullPredicate() | 用于创建一个始终检查输入是否为 null 的谓词 |
| onePredicate(Collection<? extends Predicate<?>> predicates) onePredicate(Predicate<?>... predicates) | 用于创建一个仅当恰好一个子谓词返回 true 时才返回 true 的组合谓词 |
| orPredicate(Predicate<?> predicate1, Predicate<?> predicate2) | 用于创建一个当任意一个子谓词返回 true 时即返回 true 的组合谓词 |
| transformedPredicate(Transformer<?, ?> transformer, Predicate<?> predicate) | 用于创建一个先对输入对象进行转换(Transform),再将转换结果传递给指定谓词(Predicate)进行判断的组合谓词1 |
| uniquePredicate() | 用于创建一个确保集合中元素唯一性的谓词 |
QueueUtils
| 方法 | 说明 |
|---|---|
| emptyQueue() | 用于返回一个空队列 |
| predicatedQueue(Queue<E> queue, Predicate<? super E> predicate) | 用于返回一个基于断言(Predicate)的验证队列 |
| synchronizedQueue(Queue<E> queue) | 用于返回一个线程安全的队列包装器 |
| transformingQueue(Queue<E> queue, Transformer<? super E, ? extends E> transformer) | 用于返回一个基于转换器的队列包装器 |
| unmodifiableQueue(Queue<? extends E> queue) | 用于返回一个不可修改的队列包装器 |
SetUtils
| 方法 | 说明 |
|---|---|
| difference(final Set<?> setA, final Set<?> setB) | 用于计算两个集合的差集 |
| disjunction(final Set<?> setA, final Set<?> setB) | 用于计算两个集合的对称差集 |
| emptyIfNull(Set<T> set) | 用于处理可能为 null 的集合 |
| emptySet() | 获取空集合 |
| emptySortedSet() | 获取空有序集合 |
| hashCodeForSet(Collection<T> set) | 用于计算集合的哈希值 |
| hashSet(E... items) | 用于快速创建不可修改的 HashSet |
| intersection(final Set<?> setA, final Set<?> setB) | 用于返回两个集合的不可修改交集 |
| isEqualSet(Collection<?> set1, Collection<?> set2) | 用于判断两个集合是否包含相同的元素(不考虑顺序和重复) |
| newIdentityHashSet() | 用于创建一个基于对象引用(==)而非 equals() 方法比较元素的新 HashSet |
| orderedSet(Set<E> set) | 用于返回一个按元素添加顺序保留的 Set |
| predicatedNavigableSet(NavigableSet<E> set, Predicate<? super E> predicate) | 用于返回一个基于谓词(Predicate)校验的不可修改导航集合 |
| predicatedSet(Set<E> set, Predicate<? super E> predicate) | 用于返回一个基于谓词校验的不可修改集合 |
| predicatedSortedSet(SortedSet<E> set, Predicate<? super E> predicate) | 用于返回一个基于谓词校验的不可修改有序集合 |
| synchronizedSet(Set<E> set) | 用于返回一个线程安全的同步集合 |
| synchronizedSortedSet(SortedSet<E> set) | 用于返回一个线程安全的同步有序集合 |
| transformedNavigableSet(NavigableSet<E> set, Transformer<? super E, ?> transformer) | 用于返回一个基于转换器的可变导航集合 |
| transformedSet(Set<E> set, Transformer<? super E, ?> transformer) | 用于返回一个基于转换器的可变集合 |
| transformedSortedSet(SortedSet<E> set, Transformer<? super E, ?> transformer) | 用于返回一个基于转换器的可变有序集合 |
| union(final Set<?> setA, final Set<?> setB) | 用于合并两个集合并返回包含所有元素的新集合(去重)。 |
| unmodifiableNavigableSet(NavigableSet<E> set) | 用于返回一个不可修改的 NavigableSet |
| unmodifiableSet(E... items) unmodifiableSet(Set<?> set) | 用于返回一个不可修改的 Set |
| unmodifiableSortedSet(SortedSet<E> set) | 用于返回一个不可修改的 SortedSet |
SplitMapUtils 用于处理"分离Map"的工具类,提供将Get/Put接口转换为标准Map接口的方法
| 方法 | 说明 |
|---|---|
| readableMap(Get<K, V> get) | 用于将实现了 Get 接口的对象转换为 IterableMap 实例。 |
| writableMap(Put<K, V> put) | 用于将实现了 Put 接口的对象转换为 Map 实例 |
TransformerUtils 用于简化Transformer对象的创建和组合操作
| 方法 | 说明 |
|---|---|
| asTransformer(Closure<?> closure) asTransformer(Factory<?> factory) asTransformer(Predicate<?> predicate) | 用于将普通函数或Lambda表达式转换为Transformer接口实例 |
| chainedTransformer(Collection<?ransformer<?, ?>> transformers) chainedTransformer(Transformer<?, ?>... transformers) | 用于将多个Transformer实现串联执行,形成一条转换链 |
| cloneTransformer() | 克隆对象 |
| constantTransformer(O constantToReturn) | 用于创建始终返回固定值的转换器 |
| exceptionTransformer() | 用于捕获异常并转换为特定结果的Transformer实现,通常用于链式操作中处理异常场景。 |
| ifTransformer(Predicate<?> predicate, Transformer<?, ?> trueTransformer, Transformer<?, ?> falseTransformer) ifTransformer(Predicate<?> predicate, Transformer<?, ?> trueTransformer) | 用于根据输入值动态选择不同的转换逻辑 |
| instantiateTransformer() instantiateTransformer(Class<?>[] paramTypes, Object[] args) | 用于创建始终返回固定值的转换器 |
| invokerTransformer(String methodName) invokerTransformer(String methodName, Class<?>[] paramTypes, Object[] args) | 用于通过反射动态调用任意方法的工具类,常被利用于反序列化攻击链(如CC链)中执行恶意代码 |
| mapTransformer(Map<?, ?> map) | 用于将Map的键值对转换为其他对象的转换器,通常用于键值对的映射转换场景。 |
| nopTransformer() | 直接返回输入对象而不做任何修改的Transformer |
| nullTransformer() | 始终返回null的Transformer |
| stringValueTransformer() | 用于将输入对象转换为字符串表示形式的Transformer |
| switchMapTransformer(Map<I, Transformer<I, O>> objectsAndTransformers) | 通过Predicate数组和Transformer数组的顺序匹配实现键值对的条件转换 |
| switchTransformer(Map<Predicate*, Transformer<I, O>> predicatesAndTransformers) switchTransformer(Predicate<?>[] predicates, Transformer<?, ?>[] transformers) switchTransformer(Predicate<?>[] predicates, Transformer<?, ?>[] transformers, Transformer<?, ?> defaultTransformer)* | 通过Predicate数组和Transformer数组的顺序匹配实现输入对象的条件转换。 |
TrieUtils
| 方法 | 说明 |
|---|---|
| unmodifiableTrie(Trie<K, ?> trie) | 用于返回一个不可修改的 Trie 实例,任何修改操作(如添加/删除键值对)都会抛出异常 |