Package org.miaixz.bus.core.xyz
Class CollKit
java.lang.Object
org.miaixz.bus.core.center.CollectionStream
org.miaixz.bus.core.xyz.CollKit
集合相关工具类
此工具方法针对
Collection或Iterable及其实现类封装的工具- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collection<T> addAll(Collection<T> collection, Iterable<T> iterable) 加入全部static <T> Collection<T> addAll(Collection<T> collection, Object value) 将指定对象全部加入到集合中 提供的对象如果为集合类型,会自动转换为目标元素类型static <T> Collection<T> addAll(Collection<T> collection, Object value, Type elementType) 将指定对象全部加入到集合中 提供的对象如果为集合类型,会自动转换为目标元素类型 如果为String,支持类似于[1,2,3,4] 或者 1,2,3,4 这种格式static <T> Collection<T> addAll(Collection<T> collection, Enumeration<T> enumeration) 加入全部static <T> Collection<T> addAll(Collection<T> collection, Iterator<T> iterator) 加入全部static <T> Collection<T> addAll(Collection<T> collection, T[] values) 加入全部static <T,S extends T>
booleanaddIfAbsent(Collection<T> collection, S object) 一个对象不为空且不存在于该集合中时,加入到该集合中static <T> booleanallMatch(Collection<T> collection, Predicate<T> predicate) 是否全部匹配判断条件static <T> booleananyMatch(Collection<T> collection, Predicate<T> predicate) 是否至少有一个符合判断条件static <E> Enumeration<E> asEnumeration(Iterator<E> iter) Iterator转换为Enumerationstatic voidclear(Collection<?>... collections) 清除一个或多个集合内的元素,每个集合调用clear()方法static booleancontains(Collection<?> collection, Object value) 判断指定集合是否包含指定值,如果集合为空(null或者空),返回false,否则找到元素返回truestatic <T> booleancontains(Collection<T> collection, Predicate<? super T> containFunc) 自定义函数判断集合是否包含某类值static booleancontainsAll(Collection<?> coll1, Collection<?> coll2) 集合1中是否包含集合2中所有的元素。 当集合1和集合2都为空时,返回true当集合2为空时,返回truestatic booleancontainsAny(Collection<?> coll1, Collection<?> coll2) 其中一个集合在另一个集合中是否至少包含一个元素,即是两个集合是否至少有一个共同的元素static <T> int集合中匹配规则的数量根据集合返回一个元素计数的Map所谓元素计数就是假如这个集合中某个元素出现了n次,那将这个元素做为key,n做为value 例如:[a,b,c,c,c] 得到: a: 1 b: 1 c: 3static <T> Collection<T> 创建新的集合对象,返回具体的泛型集合static <T> Collection<T> 创建新的集合对象,返回具体的泛型集合static <T extends Collection<E>,E>
TdefaultIfEmpty(T collection, Function<T, T> handler, Supplier<? extends T> defaultSupplier) 如果给定集合为空,返回默认集合static <T extends Collection<E>,E>
TdefaultIfEmpty(T collection, T defaultCollection) 如果给定集合为空,返回默认集合static <T> Collection<T> disjunction(Collection<T> coll1, Collection<T> coll2) 两个集合的差集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留两个集合中此元素个数差的个数 例如:static <T> List<T> distinct(Collection<T> collection) 去重集合static <T,K> List <T> distinct(Collection<T> collection, Function<T, K> key, boolean override) 根据函数生成的KEY去重集合,如根据Bean的某个或者某些字段完成去重。 去重可选是保留最先加入的值还是后加入的值static <T> Predicate<T> 去重集合static <T extends Collection<E>,E>
Tedit(T collection, UnaryOperator<E> editor) 编辑,此方法产生一个新集合 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:static <E,T extends Collection<E>>
T根据给定的集合类型,返回对应的空集合,支持类型包括:static <T> List<T> emptyIfNull(List<T> list) 如果提供的集合为null,返回一个不可变的默认空集合,否则返回原集合 空集合使用Collections.emptyList()static <T> Set<T> emptyIfNull(Set<T> set) 如果提供的集合为null,返回一个不可变的默认空集合,否则返回原集合 空集合使用Collections.emptySet()static <K,V> Map <K, V> fieldValueAsMap(Iterable<?> iterable, String fieldNameForKey, String fieldNameForValue) 两个字段值组成新的Mapstatic <K,V> Map <K, V> fieldValueMap(Iterable<V> iterable, String fieldName) 字段值与列表值对应的Map,常用于元素对象中有唯一ID时需要按照这个ID查找对象的情况 例如:车牌号 = 车static <T extends Collection<E>,E>
T过滤 过滤过程通过传入的Predicate实现来过滤返回需要的元素内容,可以实现以下功能:static <T> List<T> flat(Collection<?> collection) 解构多层集合 例如:List<List<List<String>>> 解构成 List<String>static <T> List<T> flat(Collection<?> collection, boolean skipNull) 解构多层集合 例如:List<List<List<String>>> 解构成 List<String>static <T> voidforEach(Iterable<T> iterable, BiConsumerX<T, Integer> consumer) 循环遍历Iterable,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理static <T> voidforEach(Enumeration<T> enumeration, BiConsumerX<T, Integer> consumer) 循环遍历Enumeration,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理static <T> voidforEach(Iterator<T> iterator, BiConsumerX<T, Integer> consumer) 循环遍历Iterator,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理static <K,V> void forEach(Map<K, V> map, SerConsumer3<K, V, Integer> kvConsumer) 循环遍历Map,使用SerConsumer3接受遍历的每条数据,并针对每条数据做处理 和JDK8中的map.forEach不同的是,此方法支持indexstatic <T> Tget(Collection<T> collection, int index) 获取集合中指定下标的元素值,下标可以为负数,例如-1表示最后一个元素 如果元素越界,返回nullstatic <T> List<T> getAny(Collection<T> collection, int... indexes) 获取集合中指定多个下标的元素值,下标可以为负数,例如-1表示最后一个元素static Collection<Object> getFieldValues(Iterable<?> collection, String fieldName) 获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与MapgetFieldValues(Iterable<?> collection, String fieldName, boolean ignoreNull) 获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与Mapstatic <T> List<T> getFieldValues(Iterable<?> collection, String fieldName, Class<T> elementType) 获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与Mapstatic <T> T获取集合的第一个元素,如果集合为空(null或者空集合),返回nullstatic <T> T查找第一个匹配元素对象static <T> TgetFirstByField(Iterable<T> collection, String fieldName, Object fieldValue) 查找第一个匹配元素对象 如果集合元素是Map,则比对键和值是否相同,相同则返回 如果为普通Bean,则通过反射比对元素字段名对应的字段值是否相同,相同则返回 如果给定字段值参数是null且元素对象中的字段值也为null则认为相同static <T> TgetFirstNoneNull(Iterable<T> iterable) 获取集合的第一个非空元素static <T> TgetLast(Collection<T> collection) 获取集合的最后一个元素group(Collection<T> collection, Hash32<T> hash) 分组,按照Hash32接口定义的hash算法,集合中的元素放入hash值对应的子列表中groupByField(Collection<T> collection, String fieldName) 根据元素的指定字段值分组,非Bean都放在第一个分组中groupByFunc(Collection<T> collection, Function<T, ?> getter) 根据元素的指定字段值分组,非Bean都放在第一个分组中 例如:CollKit.groupByFunc(list, TestBean::getAge)static boolean是否包含null元素 集合为null,返回true集合为空集合,即元素个数为0,返回false集合中元素为"",返回falseindexListOfAll(Collection<T> collection, Predicate<T> predicate) 获取匹配规则定义中匹配到元素的所有位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。static <T> intindexOf(Collection<T> collection, Predicate<T> predicate) 获取匹配规则定义中匹配到元素的第一个位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。static <T> int[]indexOfAll(Collection<T> collection, Predicate<T> predicate) 获取匹配规则定义中匹配到元素的所有位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。static <T> Collection<T> intersection(Collection<T>... colls) 多个集合的交集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最少的个数 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c],此结果中只保留了两个cstatic <T> Set<T> intersectionDistinct(Collection<T>... colls) 多个集合的交集 针对一个集合中存在多个相同元素的情况,只保留一个 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c],此结果中只保留了一个cstatic booleanIterable是否为空static booleanisEmpty(Collection<?> collection) 集合是否为空static booleanisEmpty(Enumeration<?> enumeration) Enumeration是否为空static booleanIterator是否为空static booleanMap是否为空static booleanisEqualList(Collection<?> list1, Collection<?> list2) 判断两个Collection是否元素和顺序相同,返回true的条件是: 两个Collection必须长度相同 两个Collection元素相同index的对象必须equals,满足Objects.equals(Object, Object)此方法来自Apache-Commons-Collections4。static booleanisNotEmpty(Iterable<?> iterable) Iterable是否为空static booleanisNotEmpty(Collection<?> collection) 集合是否为非空static booleanisNotEmpty(Enumeration<?> enumeration) Enumeration是否为空static booleanisNotEmpty(Iterator<?> iterator) Iterator是否为空static booleanisNotEmpty(Map<?, ?> map) Map是否为非空static <T> Stringjoin(Iterable<T> iterable, CharSequence conjunction) static <T> Stringjoin(Iterable<T> iterable, CharSequence conjunction, String prefix, String suffix) 以 conjunction 为分隔符将集合转换为字符串static <T> Stringjoin(Iterable<T> iterable, CharSequence conjunction, Function<T, ? extends CharSequence> func) 以 conjunction 为分隔符将集合转换为字符串static <K> Set<K> keySet(Collection<Map<K, ?>> mapCollection) 获取指定Map列表中所有的Keystatic <T> intlastIndexOf(Collection<T> collection, Predicate<? super T> predicate) 获取匹配规则定义中匹配到元素的最后位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。static <T,R> List <R> 通过func自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回 例如提供的是一个Bean列表,通过Function接口实现获取某个字段值,返回这个字段值组成的新列表 默认忽略映射后null的情况static <T,R> List <R> 通过func自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回 例如提供的是一个Bean列表,通过Function接口实现获取某个字段值,返回这个字段值组成的新列表static <T extends Comparable<? super T>>
Tmax(Collection<T> coll) 取最大值static <T extends Comparable<? super T>>
Tmin(Collection<T> coll) 取最小值static <T> BlockingQueue<T> newBlockingQueue(int capacity, boolean isLinked) 新建BlockingQueue在队列为空时,获取元素的线程会等待队列变为非空。当队列满时,存储元素的线程会等待队列可用。static <T> void填充List,以达到最小长度static <T> voidpadRight(Collection<T> list, int minLen, T padObj) 填充List,以达到最小长度partition(Collection<T> collection, int size) 对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表static <T> List<T> 切取部分数据 切取后的栈将减少这些元素static <T> List<T> 切取部分数据 切取后的栈将减少这些元素static <T extends Iterable<E>,E>
T移除集合中满足条件的所有元素,此方法在原集合上直接修改 通过实现Predicate接口,完成元素的移除,可以实现以下功能:static <T extends Collection<E>,E>
TremoveAny(T collection, E... elesRemoved) 去掉集合中的多个元素,此方法直接修改原集合static <T extends Collection<E>,E extends CharSequence>
TremoveBlank(T collection) 去除null或者""或者空白字符串 元素,此方法直接修改原集合static <T extends Collection<E>,E extends CharSequence>
TremoveEmpty(T collection) 去除null或者"" 元素,此方法直接修改原集合static <T extends Collection<E>,E>
TremoveNull(T collection) 去除null元素,此方法直接修改原集合static <T extends Collection<E>,E>
List<E> removeWithAddIf(T targetCollection, Predicate<? super E> predicate) 移除集合中的多个元素,并将结果存放到生成的新集合中后返回 此方法直接修改原集合static <T extends Collection<E>,E>
TremoveWithAddIf(T targetCollection, T resultCollection, Predicate<? super E> predicate) 移除集合中的多个元素,并将结果存放到指定的集合 此方法直接修改原集合static intringNextInt(int modulo, AtomicInteger atomicInteger) 通过cas操作 实现对指定值内的回环累加static intringNextIntByObject(Object object, AtomicInteger atomicInteger) 通过cas操作 实现对指定值内的回环累加static longringNextLong(long modulo, AtomicLong atomicLong) 通过cas操作 实现对指定值内的回环累加 此方法一般用于大量数据完成回环累加(如数据库中的值大于int最大值)static booleansafeContains(Collection<?> collection, Object value) 判断指定集合是否包含指定值,如果集合为空(null或者空),返回false,否则找到元素返回truestatic <E,K, V> void setValueByMap(Iterable<E> iterable, Map<K, V> map, Function<E, K> keyGenerate, BiConsumer<E, V> biConsumer) 使用给定的map将集合中的元素进行属性或者值的重新设定static int获取Collection或者iterator的大小,此方法可以处理的对象类型如下: Collection - the collection size Map - the map size Array - the array size Iterator - the number of elements remaining in the iterator Enumeration - the number of elements remaining in the enumerationstatic <T> List<T> sort(Collection<T> collection, Comparator<? super T> comparator) 排序集合,排序不会修改原集合static <T> List<T> sort(List<T> list, Comparator<? super T> c) 针对List排序,排序会修改原Liststatic <K,V> TreeMap <K, V> sort(Map<K, V> map, Comparator<? super K> comparator) 排序Mapstatic <K,V> LinkedHashMap <K, V> sortByEntry(Map<K, V> map, Comparator<Map.Entry<K, V>> comparator) 通过Entry排序,可以按照键排序,也可以按照值排序,亦或者两者综合排序sortByPinyin(Collection<String> collection) 根据汉字的拼音顺序排序sortByPinyin(List<String> list) 根据汉字的拼音顺序排序static <T> List<T> sortByProperty(Collection<T> collection, String property) 根据Bean的属性排序static <T> List<T> sortByProperty(List<T> list, String property) 根据Bean的属性排序sortEntryToList(Collection<Map.Entry<K, V>> collection) 将Set排序(根据Entry的值)static <K,V> LinkedHashMap <K, V> sortToMap(Collection<Map.Entry<K, V>> entryCollection, Comparator<Map.Entry<K, V>> comparator) 通过Entry排序,可以按照键排序,也可以按照值排序,亦或者两者综合排序static <T> List<T> sub(Collection<T> collection, int start, int end) 截取集合的部分static <T> List<T> sub(Collection<T> collection, int start, int end, int step) 截取集合的部分static <T> List<T> 截取列表的部分static <T> List<T> 截取列表的部分static <T> Collection<T> subtract(Collection<T> coll1, Collection<T> coll2) 计算集合的单差集,即只返回【集合1】中有,但是【集合2】中没有的元素,例如:static <T> List<T> subtractToList(Collection<T> coll1, Collection<T> coll2) 计算集合的单差集,即只返回【集合1】中有,但是【集合2】中没有的元素,例如:static <E> Collection<E> toCollection(Iterable<E> iterable) 行转列,合并相同的键,值合并为列表 将Map列表中相同key的值组成列表做为Map的value 是toMapList(Map)的逆方法 比如传入数据:列转行。将Map中值列表分别按照其位置与key组成新的map。 是toListMap(Iterable)的逆方法 比如传入数据:static <T> TreeSet<T> toTreeSet(Collection<T> collection, Comparator<T> comparator) 将集合转换为排序后的TreeSetstatic <F,T> Collection <T> trans(Collection<F> collection, Function<? super F, ? extends T> function) 使用给定的转换函数,转换源集合为新类型的集合static <T> Collection<T> union(Collection<? extends T>... colls) 多个集合的并集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最多的个数 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c, c],此结果中只保留了三个cstatic <T> List<T> unionAll(Collection<? extends T>... colls) 多个集合的完全并集,类似于SQL中的“UNION ALL” 针对一个集合中存在多个相同元素的情况,保留全部元素 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c, c, a, b, c, c]static <T> Set<T> unionDistinct(Collection<? extends T>... colls) 多个集合的非重复并集,类似于SQL中的“UNION DISTINCT” 针对一个集合中存在多个相同元素的情况,只保留一个 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c],此结果中只保留了一个cstatic <V> List<V> values(Collection<Map<?, V>> mapCollection) 获取指定Map列表中所有的Valuestatic <T> Collection<T> view(Collection<? extends T> c) 转为只读集合映射键值(参考Python的zip()函数),返回Map无序 例如: keys = a,b,c,d values = 1,2,3,4 delimiter = , 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分映射键值(参考Python的zip()函数) 例如: keys = a,b,c,d values = 1,2,3,4 delimiter = , 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分static <K,V> Map <K, V> zip(Collection<K> keys, Collection<V> values) 映射键值(参考Python的zip()函数) 例如: keys = [a,b,c,d] values = [1,2,3,4] 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分Methods inherited from class org.miaixz.bus.core.center.CollectionStream
group2Map, group2Map, groupBy, groupBy, groupBy2Key, groupBy2Key, groupByKey, groupByKey, groupKeyValue, groupKeyValue, merge, toIdentityMap, toIdentityMap, toList, toList, toMap, toMap, toSet, toSet
-
Constructor Details
-
CollKit
public CollKit()
-
-
Method Details
-
isEmpty
集合是否为空- Parameters:
collection- 集合- Returns:
- 是否为空
-
isEmpty
Iterable是否为空- Parameters:
iterable- Iterable对象- Returns:
- 是否为空
- See Also:
-
isEmpty
Iterator是否为空- Parameters:
iterator- Iterator对象- Returns:
- 是否为空
- See Also:
-
isEmpty
Enumeration是否为空- Parameters:
enumeration-Enumeration- Returns:
- 是否为空
-
isEmpty
Map是否为空- Parameters:
map- 集合- Returns:
- 是否为空
- See Also:
-
defaultIfEmpty
如果给定集合为空,返回默认集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合defaultCollection- 默认数组- Returns:
- 非空(empty)的原集合或默认集合
-
defaultIfEmpty
public static <T extends Collection<E>,E> T defaultIfEmpty(T collection, Function<T, T> handler, Supplier<? extends T> defaultSupplier) 如果给定集合为空,返回默认集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合handler- 非空的处理函数defaultSupplier- 默认值懒加载函数- Returns:
- 非空(empty)的原集合或默认集合
-
emptyIfNull
如果提供的集合为null,返回一个不可变的默认空集合,否则返回原集合 空集合使用Collections.emptySet()- Type Parameters:
T- 集合元素类型- Parameters:
set- 提供的集合,可能为null- Returns:
- 原集合,若为null返回空集合
-
emptyIfNull
如果提供的集合为null,返回一个不可变的默认空集合,否则返回原集合 空集合使用Collections.emptyList()- Type Parameters:
T- 集合元素类型- Parameters:
list- 提供的集合,可能为null- Returns:
- 原集合,若为null返回空集合
-
isNotEmpty
集合是否为非空- Parameters:
collection- 集合- Returns:
- 是否为非空
-
isNotEmpty
Iterable是否为空- Parameters:
iterable- Iterable对象- Returns:
- 是否为空
- See Also:
-
isNotEmpty
Iterator是否为空- Parameters:
iterator- Iterator对象- Returns:
- 是否为空
- See Also:
-
isNotEmpty
Enumeration是否为空- Parameters:
enumeration-Enumeration- Returns:
- 是否为空
-
hasNull
是否包含null元素- 集合为
null,返回true - 集合为空集合,即元素个数为0,返回
false - 集合中元素为"",返回
false
- Parameters:
iterable- 被检查的Iterable对象,如果为null返回true- Returns:
- 是否包含
null元素 - See Also:
- 集合为
-
isNotEmpty
Map是否为非空- Parameters:
map- 集合- Returns:
- 是否为非空
- See Also:
-
distinct
去重集合- Type Parameters:
T- 集合元素类型- Parameters:
key- 属性名- Returns:
List
-
distinct
去重集合- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合- Returns:
List
-
distinct
public static <T,K> List<T> distinct(Collection<T> collection, Function<T, K> key, boolean override) 根据函数生成的KEY去重集合,如根据Bean的某个或者某些字段完成去重。 去重可选是保留最先加入的值还是后加入的值- Type Parameters:
T- 集合元素类型K- 唯一键类型- Parameters:
collection- 集合key- 唯一标识override- 是否覆盖模式,如果为true,加入的新值会覆盖相同key的旧值,否则会忽略新加值- Returns:
List
-
union
多个集合的并集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最多的个数 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c, c],此结果中只保留了三个c- Type Parameters:
T- 集合元素类型- Parameters:
colls- 集合数组- Returns:
- 并集的集合,返回
ArrayList
-
unionDistinct
多个集合的非重复并集,类似于SQL中的“UNION DISTINCT” 针对一个集合中存在多个相同元素的情况,只保留一个 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c],此结果中只保留了一个c- Type Parameters:
T- 集合元素类型- Parameters:
colls- 列表集合- Returns:
- 并集的集合,返回
LinkedHashSet
-
unionAll
多个集合的完全并集,类似于SQL中的“UNION ALL” 针对一个集合中存在多个相同元素的情况,保留全部元素 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c, c, a, b, c, c]- Type Parameters:
T- 集合元素类型- Parameters:
colls- 集合数组- Returns:
- 并集的集合,返回
ArrayList
-
intersection
多个集合的交集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最少的个数 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c, c],此结果中只保留了两个c- Type Parameters:
T- 集合元素类型- Parameters:
colls- 集合列表- Returns:
- 交集的集合,返回
ArrayList
-
intersectionDistinct
多个集合的交集 针对一个集合中存在多个相同元素的情况,只保留一个 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c] 结果:[a, b, c],此结果中只保留了一个c- Type Parameters:
T- 集合元素类型- Parameters:
colls- 集合列表- Returns:
- 交集的集合,返回
LinkedHashSet
-
disjunction
两个集合的差集 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留两个集合中此元素个数差的个数 例如:disjunction([a, b, c, c, c], [a, b, c, c]) - [c] disjunction([a, b], []) - [a, b] disjunction([a, b, c], [b, c, d]) - [a, d]任意一个集合为空,返回另一个集合 两个集合无差集则返回空集合- Type Parameters:
T- 集合元素类型- Parameters:
coll1- 集合1coll2- 集合2- Returns:
- 差集的集合,返回
ArrayList
-
subtract
计算集合的单差集,即只返回【集合1】中有,但是【集合2】中没有的元素,例如:subtract([1,2,3,4],[2,3,4,5]) - [1]- Type Parameters:
T- 元素类型- Parameters:
coll1- 集合1coll2- 集合2- Returns:
- 单差集
-
subtractToList
计算集合的单差集,即只返回【集合1】中有,但是【集合2】中没有的元素,例如:subtractToList([1,2,3,4],[2,3,4,5]) - [1]- Type Parameters:
T- 元素类型- Parameters:
coll1- 集合1coll2- 集合2- Returns:
- 单差集
-
contains
判断指定集合是否包含指定值,如果集合为空(null或者空),返回false,否则找到元素返回true- Parameters:
collection- 集合value- 需要查找的值- Returns:
- 如果集合为空(null或者空),返回
false,否则找到元素返回true - Throws:
ClassCastException- 如果类型不一致会抛出转换异常NullPointerException- 当指定的元素 值为 null ,或集合类不支持null 时抛出该异常- See Also:
-
safeContains
判断指定集合是否包含指定值,如果集合为空(null或者空),返回false,否则找到元素返回true- Parameters:
collection- 集合value- 需要查找的值- Returns:
- 果集合为空(null或者空),返回
false,否则找到元素返回true
-
contains
自定义函数判断集合是否包含某类值- Type Parameters:
T- 值类型- Parameters:
collection- 集合containFunc- 自定义判断函数- Returns:
- 是否包含自定义规则的值
-
containsAny
其中一个集合在另一个集合中是否至少包含一个元素,即是两个集合是否至少有一个共同的元素- Parameters:
coll1- 集合1coll2- 集合2- Returns:
- 两个集合是否至少有一个共同的元素
- See Also:
-
containsAll
集合1中是否包含集合2中所有的元素。 当集合1和集合2都为空时,返回true当集合2为空时,返回true- Parameters:
coll1- 集合1coll2- 集合2- Returns:
- 集合1中是否包含集合2中所有的元素
-
countMap
根据集合返回一个元素计数的Map所谓元素计数就是假如这个集合中某个元素出现了n次,那将这个元素做为key,n做为value 例如:[a,b,c,c,c] 得到: a: 1 b: 1 c: 3- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合- Returns:
Map- See Also:
-
join
public static <T> String join(Iterable<T> iterable, CharSequence conjunction, Function<T, ? extends CharSequence> func) 以 conjunction 为分隔符将集合转换为字符串- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterableconjunction- 分隔符func- 集合元素转换器,将元素转换为字符串- Returns:
- 连接后的字符串
- See Also:
-
join
- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterableconjunction- 分隔符- Returns:
- 连接后的字符串
- See Also:
-
join
public static <T> String join(Iterable<T> iterable, CharSequence conjunction, String prefix, String suffix) 以 conjunction 为分隔符将集合转换为字符串- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterableconjunction- 分隔符prefix- 每个元素添加的前缀,null表示不添加suffix- 每个元素添加的后缀,null表示不添加- Returns:
- 连接后的字符串
-
popPart
切取部分数据 切取后的栈将减少这些元素- Type Parameters:
T- 集合元素类型- Parameters:
surplusAlaDatas- 原数据partSize- 每部分数据的长度- Returns:
- 切取出的数据或null
-
popPart
切取部分数据 切取后的栈将减少这些元素- Type Parameters:
T- 集合元素类型- Parameters:
surplusAlaDatas- 原数据partSize- 每部分数据的长度- Returns:
- 切取出的数据或null
-
newBlockingQueue
新建BlockingQueue在队列为空时,获取元素的线程会等待队列变为非空。当队列满时,存储元素的线程会等待队列可用。- Type Parameters:
T- 集合类型- Parameters:
capacity- 容量isLinked- 是否为链表形式- Returns:
BlockingQueue
-
empty
根据给定的集合类型,返回对应的空集合,支持类型包括:1. NavigableSet 2. SortedSet 3. Set 4. List- Type Parameters:
E- 元素类型T- 集合类型- Parameters:
collectionClass- 集合类型- Returns:
- 空集合
-
create
创建新的集合对象,返回具体的泛型集合- Type Parameters:
T- 集合元素类型,rawtype 如 ArrayList.class, EnumMap.class ...- Parameters:
collectionType- 集合类型- Returns:
- 集合类型对应的实例
-
create
创建新的集合对象,返回具体的泛型集合- Type Parameters:
T- 集合元素类型,rawtype 如 ArrayList.class, EnumMap.class ...- Parameters:
collectionType- 集合类型elementType- 集合元素类,只用于EnumSet创建,如果创建EnumSet,则此参数必须非空- Returns:
- 集合类型对应的实例
-
sub
截取列表的部分- Type Parameters:
T- 集合元素类型- Parameters:
list- 被截取的数组start- 开始位置(包含)end- 结束位置(不包含)- Returns:
- 截取后的数组,当开始位置超过最大时,返回空的List
- See Also:
-
sub
截取列表的部分- Type Parameters:
T- 集合元素类型- Parameters:
list- 被截取的数组start- 开始位置(包含)end- 结束位置(不包含)step- 步进- Returns:
- 截取后的数组,当开始位置超过最大时,返回空的List
- See Also:
-
sub
截取集合的部分- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被截取的数组start- 开始位置(包含)end- 结束位置(不包含)- Returns:
- 截取后的数组,当开始位置超过最大时,返回null
-
sub
截取集合的部分- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被截取的数组start- 开始位置(包含)end- 结束位置(不包含)step- 步进- Returns:
- 截取后的数组,当开始位置超过最大时,返回空集合
-
partition
对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合size- 每个段的长度- Returns:
- 分段列表
-
edit
编辑,此方法产生一个新集合 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:1、过滤出需要的对象,如果返回null表示这个元素对象抛弃 2、修改元素对象,返回集合中为修改后的对象
- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合editor- 编辑器接口,null返回原集合- Returns:
- 过滤后的集合
-
filter
过滤 过滤过程通过传入的Predicate实现来过滤返回需要的元素内容,可以实现以下功能:1、过滤出需要的对象,
Predicate.test(Object)方法返回true的对象将被加入结果集合中- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合predicate- 过滤器,null返回原集合- Returns:
- 过滤后的数组
-
removeAny
去掉集合中的多个元素,此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合elesRemoved- 需要删除的元素- Returns:
- 原集合
-
remove
移除集合中满足条件的所有元素,此方法在原集合上直接修改 通过实现Predicate接口,完成元素的移除,可以实现以下功能:1、移除指定对象,
Predicate.test(Object)方法返回true的对象将被使用Iterator.remove()方法移除。- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
iter- 集合predicate- 过滤器接口- Returns:
- 编辑后的集合
-
removeNull
去除null元素,此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合- Returns:
- 处理后的集合
-
removeEmpty
去除null或者"" 元素,此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合- Returns:
- 处理后的集合
-
removeBlank
去除null或者""或者空白字符串 元素,此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
collection- 集合- Returns:
- 处理后的集合
-
removeWithAddIf
public static <T extends Collection<E>,E> T removeWithAddIf(T targetCollection, T resultCollection, Predicate<? super E> predicate) 移除集合中的多个元素,并将结果存放到指定的集合 此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
targetCollection- 被操作移除元素的集合resultCollection- 存放移除结果的集合predicate- 用于是否移除判断的过滤器- Returns:
- 移除结果的集合
-
removeWithAddIf
public static <T extends Collection<E>,E> List<E> removeWithAddIf(T targetCollection, Predicate<? super E> predicate) 移除集合中的多个元素,并将结果存放到生成的新集合中后返回 此方法直接修改原集合- Type Parameters:
T- 集合类型E- 集合元素类型- Parameters:
targetCollection- 被操作移除元素的集合predicate- 用于是否移除判断的过滤器- Returns:
- 移除结果的集合
-
map
通过func自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回 例如提供的是一个Bean列表,通过Function接口实现获取某个字段值,返回这个字段值组成的新列表 默认忽略映射后null的情况- Type Parameters:
T- 集合元素类型R- 返回集合元素类型- Parameters:
collection- 原集合func- 编辑函数- Returns:
- 抽取后的新列表
-
map
public static <T,R> List<R> map(Iterable<T> collection, Function<? super T, ? extends R> mapper, boolean ignoreNull) 通过func自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回 例如提供的是一个Bean列表,通过Function接口实现获取某个字段值,返回这个字段值组成的新列表- Type Parameters:
T- 集合元素类型R- 返回集合元素类型- Parameters:
collection- 原集合mapper- 编辑函数ignoreNull- 是否忽略空值,这里的空值包括函数处理前和处理后的null值- Returns:
- 抽取后的新列表
- See Also:
-
getFieldValues
获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与Map- Parameters:
collection- Bean集合或Map集合fieldName- 字段名或map的键- Returns:
- 字段值列表
-
getFieldValues
public static List<Object> getFieldValues(Iterable<?> collection, String fieldName, boolean ignoreNull) 获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与Map- Parameters:
collection- Bean集合或Map集合fieldName- 字段名或map的键ignoreNull- 是否忽略值为null的字段- Returns:
- 字段值列表
-
getFieldValues
public static <T> List<T> getFieldValues(Iterable<?> collection, String fieldName, Class<T> elementType) 获取给定Bean列表中指定字段名对应字段值的列表 列表元素支持Bean与Map- Type Parameters:
T- 元素类型- Parameters:
collection- Bean集合或Map集合fieldName- 字段名或map的键elementType- 元素类型类- Returns:
- 字段值列表
-
fieldValueMap
字段值与列表值对应的Map,常用于元素对象中有唯一ID时需要按照这个ID查找对象的情况 例如:车牌号 = 车- Type Parameters:
K- 字段名对应值得类型,不确定请使用ObjectV- 对象类型- Parameters:
iterable- 对象列表fieldName- 字段名(会通过反射获取其值)- Returns:
- 某个字段值与对象对应Map
-
fieldValueAsMap
public static <K,V> Map<K,V> fieldValueAsMap(Iterable<?> iterable, String fieldNameForKey, String fieldNameForValue) 两个字段值组成新的Map- Type Parameters:
K- 字段名对应值得类型,不确定请使用ObjectV- 值类型,不确定使用Object- Parameters:
iterable- 对象列表fieldNameForKey- 做为键的字段名(会通过反射获取其值)fieldNameForValue- 做为值的字段名(会通过反射获取其值)- Returns:
- 某个字段值与对象对应Map
-
getFirst
获取集合的第一个元素,如果集合为空(null或者空集合),返回null- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterable- Returns:
- 第一个元素,为空返回
null
-
getFirstNoneNull
获取集合的第一个非空元素- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterable- Returns:
- 第一个元素
-
getFirst
查找第一个匹配元素对象- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合predicate- 过滤器,满足过滤条件的第一个元素将被返回- Returns:
- 满足过滤条件的第一个元素
-
getFirstByField
查找第一个匹配元素对象 如果集合元素是Map,则比对键和值是否相同,相同则返回 如果为普通Bean,则通过反射比对元素字段名对应的字段值是否相同,相同则返回 如果给定字段值参数是null且元素对象中的字段值也为null则认为相同- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合,集合元素可以是Bean或者MapfieldName- 集合元素对象的字段名或map的键fieldValue- 集合元素对象的字段值或map的值- Returns:
- 满足条件的第一个元素
-
count
集合中匹配规则的数量- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterablepredicate- 匹配器,为空则全部匹配- Returns:
- 匹配数量
-
indexOf
获取匹配规则定义中匹配到元素的第一个位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。- Type Parameters:
T- 元素类型- Parameters:
collection- 集合predicate- 匹配器,为空则全部匹配- Returns:
- 第一个位置
-
lastIndexOf
获取匹配规则定义中匹配到元素的最后位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。- Type Parameters:
T- 元素类型- Parameters:
collection- 集合predicate- 匹配器,为空则全部匹配- Returns:
- 最后一个位置
-
indexOfAll
获取匹配规则定义中匹配到元素的所有位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。- Type Parameters:
T- 元素类型- Parameters:
collection- 集合predicate- 匹配器,为空则全部匹配- Returns:
- 位置数组
-
indexListOfAll
获取匹配规则定义中匹配到元素的所有位置 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。- Type Parameters:
T- 元素类型- Parameters:
collection- 集合predicate- 匹配器,为空则全部匹配- Returns:
- 位置数组
-
zip
映射键值(参考Python的zip()函数) 例如: keys = a,b,c,d values = 1,2,3,4 delimiter = , 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分- Parameters:
keys- 键列表values- 值列表delimiter- 分隔符isOrder- 是否有序- Returns:
- Map
-
zip
映射键值(参考Python的zip()函数),返回Map无序 例如: keys = a,b,c,d values = 1,2,3,4 delimiter = , 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分- Parameters:
keys- 键列表values- 值列表delimiter- 分隔符- Returns:
- Map
-
zip
映射键值(参考Python的zip()函数) 例如: keys = [a,b,c,d] values = [1,2,3,4] 则得到的Map是 {a=1, b=2, c=3, d=4} 如果两个数组长度不同,则只对应最短部分- Type Parameters:
K- 键类型V- 值类型- Parameters:
keys- 键列表values- 值列表- Returns:
- Map
-
toTreeSet
将集合转换为排序后的TreeSet- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合comparator- 比较器- Returns:
- treeSet
-
asEnumeration
Iterator转换为EnumerationAdapt the specified
Iteratorto theEnumerationinterface.- Type Parameters:
E- 集合元素类型- Parameters:
iter-Iterator- Returns:
Enumeration
-
toCollection
- Type Parameters:
E- 集合元素类型- Parameters:
iterable-Iterable- Returns:
Collection或者ArrayList
-
toListMap
行转列,合并相同的键,值合并为列表 将Map列表中相同key的值组成列表做为Map的value 是toMapList(Map)的逆方法 比如传入数据:[ {a: 1, b: 1, c: 1} {a: 2, b: 2} {a: 3, b: 3} {a: 4} ]结果是:
{ a: [1,2,3,4] b: [1,2,3,] c: [1] }- Type Parameters:
K- 键类型V- 值类型- Parameters:
mapList- Map列表- Returns:
- Map
- See Also:
-
toMapList
列转行。将Map中值列表分别按照其位置与key组成新的map。 是toListMap(Iterable)的逆方法 比如传入数据:{ a: [1,2,3,4] b: [1,2,3,] c: [1] }结果是:
[ {a: 1, b: 1, c: 1} {a: 2, b: 2} {a: 3, b: 3} {a: 4} ]- Type Parameters:
K- 键类型V- 值类型- Parameters:
listMap- 列表Map- Returns:
- Map列表
- See Also:
-
addAll
将指定对象全部加入到集合中 提供的对象如果为集合类型,会自动转换为目标元素类型- Type Parameters:
T- 元素类型- Parameters:
collection- 被加入的集合value- 对象,可能为Iterator、Iterable、Enumeration、Array- Returns:
- 被加入集合
-
addAll
将指定对象全部加入到集合中 提供的对象如果为集合类型,会自动转换为目标元素类型 如果为String,支持类似于[1,2,3,4] 或者 1,2,3,4 这种格式- Type Parameters:
T- 元素类型- Parameters:
collection- 被加入的集合value- 对象,可能为Iterator、Iterable、Enumeration、Array,或者与集合元素类型一致elementType- 元素类型,为空时,使用Object类型来接纳所有类型- Returns:
- 被加入集合
-
addAll
加入全部- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被加入的集合Collectioniterator- 要加入的Iterator- Returns:
- 原集合
-
addAll
加入全部- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被加入的集合Collectioniterable- 要加入的内容Iterable- Returns:
- 原集合
-
addAll
加入全部- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被加入的集合Collectionenumeration- 要加入的内容Enumeration- Returns:
- 原集合
-
addAll
加入全部- Type Parameters:
T- 集合元素类型- Parameters:
collection- 被加入的集合Collectionvalues- 要加入的内容数组- Returns:
- 原集合
-
getLast
获取集合的最后一个元素- Type Parameters:
T- 集合元素类型- Parameters:
collection-Collection- Returns:
- 最后一个元素
-
get
获取集合中指定下标的元素值,下标可以为负数,例如-1表示最后一个元素 如果元素越界,返回null- Type Parameters:
T- 元素类型- Parameters:
collection- 集合index- 下标,支持负数- Returns:
- 元素值
-
getAny
获取集合中指定多个下标的元素值,下标可以为负数,例如-1表示最后一个元素- Type Parameters:
T- 元素类型- Parameters:
collection- 集合indexes- 下标,支持负数- Returns:
- 元素值列表
-
sort
排序集合,排序不会修改原集合- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合comparator- 比较器- Returns:
- treeSet
-
sort
针对List排序,排序会修改原List- Type Parameters:
T- 元素类型- Parameters:
list- 被排序的Listc-Comparator- Returns:
- 原list
- See Also:
-
sortByProperty
根据Bean的属性排序- Type Parameters:
T- 元素类型- Parameters:
collection- 集合,会被转换为Listproperty- 属性名- Returns:
- 排序后的List
-
sortByProperty
根据Bean的属性排序- Type Parameters:
T- 元素类型- Parameters:
list- Listproperty- 属性名- Returns:
- 排序后的List
-
sortByPinyin
根据汉字的拼音顺序排序- Parameters:
collection- 集合,会被转换为List- Returns:
- 排序后的List
-
sortByPinyin
根据汉字的拼音顺序排序- Parameters:
list- List- Returns:
- 排序后的List
-
sort
排序Map- Type Parameters:
K- 键类型V- 值类型- Parameters:
map- Mapcomparator- Entry比较器- Returns:
TreeMap
-
sortToMap
public static <K,V> LinkedHashMap<K,V> sortToMap(Collection<Map.Entry<K, V>> entryCollection, Comparator<Map.Entry<K, V>> comparator) 通过Entry排序,可以按照键排序,也可以按照值排序,亦或者两者综合排序- Type Parameters:
K- 键类型V- 值类型- Parameters:
entryCollection- Entry集合comparator-Comparator- Returns:
LinkedList
-
sortByEntry
public static <K,V> LinkedHashMap<K,V> sortByEntry(Map<K, V> map, Comparator<Map.Entry<K, V>> comparator) 通过Entry排序,可以按照键排序,也可以按照值排序,亦或者两者综合排序- Type Parameters:
K- 键类型V- 值类型- Parameters:
map- 被排序的Mapcomparator-Comparator- Returns:
LinkedList
-
sortEntryToList
将Set排序(根据Entry的值)- Type Parameters:
K- 键类型V- 值类型- Parameters:
collection- 被排序的Collection- Returns:
- 排序后的Set
-
forEach
循环遍历Iterable,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理- Type Parameters:
T- 集合元素类型- Parameters:
iterable-Iterableconsumer-BiConsumerX遍历的每条数据处理器
-
forEach
循环遍历Iterator,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理- Type Parameters:
T- 集合元素类型- Parameters:
iterator-Iteratorconsumer-BiConsumerX遍历的每条数据处理器
-
forEach
循环遍历Enumeration,使用BiConsumerX接受遍历的每条数据,并针对每条数据做处理- Type Parameters:
T- 集合元素类型- Parameters:
enumeration-Enumerationconsumer-BiConsumerX遍历的每条数据处理器
-
forEach
循环遍历Map,使用SerConsumer3接受遍历的每条数据,并针对每条数据做处理 和JDK8中的map.forEach不同的是,此方法支持index- Type Parameters:
K- Key类型V- Value类型- Parameters:
map-MapkvConsumer-SerConsumer3遍历的每条数据处理器
-
group
分组,按照Hash32接口定义的hash算法,集合中的元素放入hash值对应的子列表中- Type Parameters:
T- 元素类型- Parameters:
collection- 被分组的集合hash- Hash值算法,决定元素放在第几个分组的规则- Returns:
- 分组后的集合
-
groupByField
根据元素的指定字段值分组,非Bean都放在第一个分组中- Type Parameters:
T- 元素类型- Parameters:
collection- 集合fieldName- 元素Bean中的字段名,非Bean都放在第一个分组中- Returns:
- 分组列表
-
groupByFunc
根据元素的指定字段值分组,非Bean都放在第一个分组中 例如:CollKit.groupByFunc(list, TestBean::getAge)- Type Parameters:
T- 元素类型- Parameters:
collection- 集合getter- getter方法引用- Returns:
- 分组列表
-
keySet
获取指定Map列表中所有的Key- Type Parameters:
K- 键类型- Parameters:
mapCollection- Map列表- Returns:
- key集合
-
values
获取指定Map列表中所有的Value- Type Parameters:
V- 值类型- Parameters:
mapCollection- Map列表- Returns:
- Value集合
-
max
取最大值- Type Parameters:
T- 元素类型- Parameters:
coll- 集合- Returns:
- 最大值
- See Also:
-
min
取最小值- Type Parameters:
T- 元素类型- Parameters:
coll- 集合- Returns:
- 最小值
- See Also:
-
view
转为只读集合- Type Parameters:
T- 元素类型- Parameters:
c- 集合- Returns:
- 只读集合
-
clear
清除一个或多个集合内的元素,每个集合调用clear()方法- Parameters:
collections- 一个或多个集合
-
padLeft
填充List,以达到最小长度- Type Parameters:
T- 集合元素类型- Parameters:
list- 列表minLen- 最小长度padObj- 填充的对象
-
padRight
填充List,以达到最小长度- Type Parameters:
T- 集合元素类型- Parameters:
list- 列表minLen- 最小长度padObj- 填充的对象
-
trans
public static <F,T> Collection<T> trans(Collection<F> collection, Function<? super F, ? extends T> function) 使用给定的转换函数,转换源集合为新类型的集合- Type Parameters:
F- 源元素类型T- 目标元素类型- Parameters:
collection- 集合function- 转换函数- Returns:
- 新类型的集合
-
setValueByMap
public static <E,K, void setValueByMapV> (Iterable<E> iterable, Map<K, V> map, Function<E, K> keyGenerate, BiConsumer<E, V> biConsumer) 使用给定的map将集合中的元素进行属性或者值的重新设定- Type Parameters:
E- 元素类型K- 替换的键V- 替换的值- Parameters:
iterable- 集合map- 映射集keyGenerate- 映射键生成函数biConsumer- 封装映射到的值函数 nick_wys
-
size
获取Collection或者iterator的大小,此方法可以处理的对象类型如下:- Collection - the collection size
- Map - the map size
- Array - the array size
- Iterator - the number of elements remaining in the iterator
- Enumeration - the number of elements remaining in the enumeration
- Parameters:
object- 可以为空的对象- Returns:
- 如果object为空则返回0
- Throws:
IllegalArgumentException- 参数object不是Collection或者iterator
-
isEqualList
判断两个Collection是否元素和顺序相同,返回true的条件是:- 两个
Collection必须长度相同 - 两个
Collection元素相同index的对象必须equals,满足Objects.equals(Object, Object)
- Parameters:
list1- 列表1list2- 列表2- Returns:
- 是否相同
- 两个
-
addIfAbsent
一个对象不为空且不存在于该集合中时,加入到该集合中null, null -> false [], null -> false null, "123" -> false ["123"], "123" -> false [], "123" -> true ["456"], "123" -> true [Animal{"name": "jack"}], Dog{"name": "jack"} -> true- Type Parameters:
T- 集合元素类型S- 要添加的元素类型【为集合元素类型的类型或子类型】- Parameters:
collection- 被加入的集合object- 要添加到集合的对象- Returns:
- 是否添加成功 Cloud-Style
-
anyMatch
是否至少有一个符合判断条件- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合predicate- 自定义判断函数- Returns:
- 是否有一个值匹配 布尔值
-
allMatch
是否全部匹配判断条件- Type Parameters:
T- 集合元素类型- Parameters:
collection- 集合predicate- 自定义判断函数- Returns:
- 是否全部匹配 布尔值
-
flat
解构多层集合 例如:List<List<List<String>>> 解构成 List<String>- Type Parameters:
T- 元素类型- Parameters:
collection- 需要解构的集合- Returns:
- 解构后的集合
-
flat
解构多层集合 例如:List<List<List<String>>> 解构成 List<String>skipNull如果为true, 则解构后的集合里不包含null值,为false则会包含null值。
- Type Parameters:
T- 元素类型- Parameters:
collection- 需要结构的集合skipNull- 是否跳过空的值- Returns:
- 解构后的集合
-
ringNextIntByObject
通过cas操作 实现对指定值内的回环累加- Parameters:
object- 集合- Collection - the collection size
- Map - the map size
- Array - the array size
- Iterator - the number of elements remaining in the iterator
- Enumeration - the number of elements remaining in the enumeration
atomicInteger- 原子操作类- Returns:
- 索引位置
-
ringNextInt
通过cas操作 实现对指定值内的回环累加- Parameters:
modulo- 回环周期值atomicInteger- 原子操作类- Returns:
- 索引位置
-
ringNextLong
通过cas操作 实现对指定值内的回环累加 此方法一般用于大量数据完成回环累加(如数据库中的值大于int最大值)- Parameters:
modulo- 回环周期值atomicLong- 原子操作类- Returns:
- 索引位置
-