public final class CollUtils extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static Collection<?> |
adaptToCollection(Object target)
将Object数据适配为Collection集合
null: 返回
Collections.emptyList()
Object: 适配为Collections.singletonList(Object)
Map: 适配为Map.entrySet();
Collection: 强转为Collection集合
Array: 使用Arrays.asList(Object[])转为集合;
|
static <T> boolean |
allMatch(Collection<T> source,
Predicate<T> predicate,
boolean ifEmpty)
集合中的非空元素是否全部符合条件,若集合为空,则返回false
|
static <T> boolean |
allMatch(Predicate<T> predicate,
boolean ifEmpty,
T... targets)
集合中的非空元素是否全部符合条件,若集合为空,则返回false
|
static <T> boolean |
anyMatch(Collection<T> source,
Predicate<T> predicate,
boolean ifEmpty)
集合中的非空元素是否有任意一项符合条件,若集合为空,则返回false
|
static <T> boolean |
anyMatch(Predicate<T> predicate,
boolean ifEmpty,
T... targets)
集合中的非空元素是否有任意一项符合条件,若集合为空,则返回false
|
static <T> List<T> |
asList(T... targets) |
static <T,R> void |
biForEach(Iterable<T> source1,
Iterable<R> source2,
BiConsumer<T,R> consumer)
遍历两个集合中的非空元素
|
static <T> void |
flatForEach(Iterable<Iterable<T>> source,
Consumer<T> consumer)
遍历集合中的非空元素
|
static <T> void |
forEach(Iterable<T> source,
Consumer<T> consumer)
遍历集合中的非空元素
|
static <K,V> void |
forEach(Map<K,? extends Collection<V>> source,
BiConsumer<K,V> consumer)
遍历集合中的非空元素
|
static <R,C,V> void |
forEach(Map<R,? extends Map<C,V>> source,
cn.hutool.core.lang.func.Consumer3<R,C,V> consumer)
遍历两个集合中的非空元素
|
static <T> boolean |
noneMatch(Collection<T> source,
Predicate<T> predicate,
boolean ifEmpty)
集合中的非空元素是否全部不符合条件,若集合为空,则返回true
|
static <T> boolean |
noneMatch(Predicate<T> predicate,
boolean ifEmpty,
T... targets)
集合中的非空元素是否全部不符合条件,若集合为空,则返回true
|
static <T> Stream<T> |
of(Collection<T> source) |
static <S,T,C extends Collection<T>> |
toCollection(Collection<S> source,
Supplier<C> collFactory,
Function<S,T> mapper)
将一个集合转为另一指定集合, 总是过滤转换源集合与转换后的集合中为null的元素
eg:
List<String> stringList = Arrays.asList("123", null, "424", "233", null);
System.out.println(stringList); // [123, null, 424, 233, null]
Set<Integer> integerSet = CollUtils.toCollection(stringList, LinkedHashSet::new, Integer::parseInt);
System.out.println(integerSet); // [123, 424, 233]
|
static <T,C extends Collection<T>> |
toCollection(Supplier<C> collFactory,
T... array)
将数组适配为集合
|
static <T> List<T> |
toList(Collection<T> source)
将一个集合转为ArrayList集合
|
static <T> List<T> |
toList(T... array)
将数组适配为List集合
|
static <S,T> Set<T> |
toSet(Collection<S> source,
Function<S,T> mapper)
将一个集合转为另一HashSet集合
|
static <T> Set<T> |
toSet(Collection<T> source)
将一个集合转为HashSet集合
|
static <T> Set<T> |
toSet(T... source)
将一个数组转为HashSet集合
|
public static <T> Stream<T> of(Collection<T> source)
@SafeVarargs public static <T> List<T> asList(T... targets)
public static <T> boolean anyMatch(Collection<T> source, Predicate<T> predicate, boolean ifEmpty)
source - 源集合predicate - 校验ifEmpty - 当集合为空的时返回值@SafeVarargs public static <T> boolean anyMatch(Predicate<T> predicate, boolean ifEmpty, T... targets)
targets - 源集合predicate - 校验ifEmpty - 当集合为空的时返回值public static <T> boolean allMatch(Collection<T> source, Predicate<T> predicate, boolean ifEmpty)
source - 源集合predicate - 校验ifEmpty - 当集合为空的时返回值@SafeVarargs public static <T> boolean allMatch(Predicate<T> predicate, boolean ifEmpty, T... targets)
targets - 源集合predicate - 校验ifEmpty - 当集合为空的时返回值public static <T> boolean noneMatch(Collection<T> source, Predicate<T> predicate, boolean ifEmpty)
source - 源集合predicate - 校验ifEmpty - 当集合为空的时返回值@SafeVarargs public static <T> boolean noneMatch(Predicate<T> predicate, boolean ifEmpty, T... targets)
predicate - 校验ifEmpty - 当集合为空的时返回值targets - 源集合@NonNull public static <S,T,C extends Collection<T>> C toCollection(@NonNull Collection<S> source, @NonNull Supplier<C> collFactory, @NonNull Function<S,T> mapper)
List<String> stringList = Arrays.asList("123", null, "424", "233", null);
System.out.println(stringList); // [123, null, 424, 233, null]
Set<Integer> integerSet = CollUtils.toCollection(stringList, LinkedHashSet::new, Integer::parseInt);
System.out.println(integerSet); // [123, 424, 233]
S - 源集合元素类型T - 返回集合元素类型C - 返回集合类型source - 源集合collFactory - 指定集合的生成方法, 生成的集合不能为nullmapper - 映射方法@NonNull public static <S,T> Set<T> toSet(Collection<S> source, @NonNull Function<S,T> mapper)
S - 源集合元素类型T - 返回集合元素类型source - 源集合mapper - 映射方法@NonNull public static <T> Set<T> toSet(Collection<T> source)
T - 集合元素类型source - 源集合@NonNull public static <T> List<T> toList(Collection<T> source)
T - 返回集合元素类型source - 源集合@SafeVarargs public static <T> Set<T> toSet(T... source)
T - 返回集合元素类型source - 数组public static Collection<?> adaptToCollection(Object target)
Collections.emptyList()Collections.singletonList(Object)Map.entrySet();Arrays.asList(Object[])转为集合;target - 目标数据public static <T> void forEach(Iterable<T> source, Consumer<T> consumer)
source - 源集合consumer - 操作public static <T> void flatForEach(Iterable<Iterable<T>> source, Consumer<T> consumer)
source - 源集合consumer - 操作public static <T,R> void biForEach(Iterable<T> source1, Iterable<R> source2, BiConsumer<T,R> consumer)
source1 - 源集合1source2 - 源集合2consumer - 操作public static <R,C,V> void forEach(Map<R,? extends Map<C,V>> source, cn.hutool.core.lang.func.Consumer3<R,C,V> consumer)
source - 源集合consumer - 操作public static <K,V> void forEach(Map<K,? extends Collection<V>> source, BiConsumer<K,V> consumer)
source - 源集合consumer - 操作public static <T,C extends Collection<T>> C toCollection(Supplier<C> collFactory, T... array)
collFactory - 创建集合的方法array - 数组@SafeVarargs public static <T> List<T> toList(T... array)
array - 数组Copyright © 2022. All rights reserved.