public final class CollUtils extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static Collection<?> |
adaptToCollection(Object target)
将Object数据适配为Collection集合
Object: 适配为
Collections.singletonList(Object)
Map: 获取其values集合;
Collection: 强转为Collection集合
|
static <T> @NotNull List<T> |
filter(Collection<T> source,
@NotNull Predicate<T> filter)
对集合中非空的元素过滤并将过滤得到的元素作为新集合返回,不修改源集合
|
static <K,V> @NotNull Map<K,V> |
filter(Map<K,V> source,
@NotNull BiPredicate<K,V> filter)
对集合中非空的元素过滤并将过滤得到的元素作为新集合返回,不修改源集合
|
static <T> @NotNull List<T> |
filterByReverse(Collection<T> source,
@NotNull Predicate<T> filter)
对集合集合中非空的元素过滤,并将过滤后的剩余元素作为新集合返回,不修改源集合
eg:
List<String> source = Arrays.asList("123", "456", "789", "456");
List<String> result = filterByReverse(source, s -> s.equals("456"));
System.out.println(result); // [123, 789]
|
static <K,V> @NotNull Map<K,V> |
filterByReverse(Map<K,V> source,
@NotNull BiPredicate<K,V> filter)
对集合中非空的元素过滤,并将过滤后的剩余元素作为新集合返回,不修改源集合
eg: {@code Map |
static <T> List<T> |
filterNotNull(Collection<T> source)
对集合中非空的元素过滤,返回不为null的元素,不修改源集合
|
static <S,T,C extends Collection<T>> |
flatMap(Collection<S> source,
@NotNull Function<S,? extends Collection<T>> mapper,
@NotNull Supplier<C> collFactory)
将一个集合转为二重集合,然后将其平铺为指定类型集合,总是过滤二重集合中的空集合
eg:
List<String> source = Arrays.asList("123", "421", null, "233");
Set<String> result = flatMap(source, s -> Arrays.asList(s.split("")), HashSet::new);
System.out.println(result); // [1, 2, 3, 4]
|
static <S,T,R extends Collection<T>> |
flatMap(Collection<S> source,
@NotNull Function<S,R> mapper)
将一个集合转为二重集合,然后将其平铺为ArrayList集合,总是过滤二重集合中的空集合
eg:
List<String> source = Arrays.asList("123", "421", null, "233");
List<String> result = flatMap(source, s -> Arrays.asList(s.split("")));
System.out.println(result); // [1, 2, 3, 4, 2, 1, 2, 3, 3]
|
static <T,C extends Collection<T>> |
foreach(C source,
@NotNull Consumer<T> action)
遍历集合元素并进行操作,总是过滤集合中为null的元素
|
static <K,V,M extends Map<K,V>> |
foreach(M source,
@NotNull BiConsumer<K,V> action)
遍历集合元素并进行操作,总是过滤集合中为key和value任意一者为null的元素
|
static <T> T |
getFirst(Iterable<T> target) |
static <T,K> @NotNull Map<K,List<T>> |
groupBy(Collection<T> source,
@NotNull Function<T,K> keyMapper)
将集合按指定条件分组
|
static <T,K,V,C extends Collection<V>> |
groupBy(Collection<T> source,
@NotNull Function<T,K> keyMapper,
@NotNull Function<T,V> valueMapper,
@NotNull Supplier<C> collFactory)
将集合按指定条件分组,再将对分组后得到的value集合转为另一指定类型的集合
|
static <S,K,R> @NotNull Map<K,R> |
groupByAndThen(Collection<S> source,
@NotNull Function<S,K> classify,
@NotNull BiFunction<K,List<S>,R> mergeMapper)
将集合按指定规则分组,然后得到分组结果的val集合进行映射
eg: List |
static <S,K,V,R> @NotNull Map<K,R> |
groupByAndThen(Collection<S> source,
@NotNull Function<S,K> classify,
@NotNull Function<S,V> valueMapper,
@NotNull BiFunction<K,List<V>,R> mergeMapper)
将集合按指定规则分组,然后得到分组结果的val集合转为另一种集合,最后再对转换后的val集合进行映射
eg: List |
static <S,T,K,R> @NotNull List<R> |
groupByThenFlat(List<S> source,
@NotNull Function<S,K> classify,
@NotNull Function<S,T> mapperAfterClassify,
@NotNull BiFunction<K,List<T>,R> flatMapper)
将一组元素分组后转为为另一类型集合,然后对分组结果进行映射, 总是过滤映射后集合中为null的元素
eg:
List<String> source = Arrays.asList("123", "156", "233", "244", "567");
List<String> result = groupByThenFlat(
source,
str -> str.substring(0, 1),
str -> "item: " + str,
(key, values) -> key + ": " + values.toString()
);
System.out.println(result); // 1: [item: 123, item: 156], 5: [item: 567], 2: [item: 233, item: 244]
|
static boolean |
isEmpty(Collection<?> target) |
static boolean |
isEmpty(Map<?,?> target) |
static boolean |
isNotEmpty(Collection<?> target) |
static boolean |
isNotEmpty(Map<?,?> target) |
static <T> @NotNull String |
joining(Collection<T> source,
Function<T,? extends CharSequence> charMapper)
将集合中的非空元素转为字符串,并拼接其中的非空白字符串
eg:
List<Object> objects = Arrays.asList("", null, " ", 123, 456);
System.out.println(joining(objects, String::valueOf)); // 123456
|
static <T> @NotNull String |
joining(Collection<T> source,
Function<T,? extends CharSequence> charMapper,
CharSequence delimiter,
boolean filterBank)
将集合中的非空元素转为字符串,并拼接其中的非空字符串
eg:
List<Object> objects = Arrays.asList("", null, " ", 123, 456);
System.out.println(joining(objects, String::valueOf, ", ", true)); // 123, 456
System.out.println(joining(objects, String::valueOf, ", ", false)); // , , 123, 456
|
static <K1,V,K2> Map<K2,V> |
mapKeys(Map<K1,V> source,
Function<K1,K2> keyMapper)
将源集合的value映射到新的key上
|
static <K,V1,V2> Map<K,V2> |
mapValues(Map<K,V1> source,
Function<V1,V2> valueMapper)
将源集合的value映射到新的key上
|
static <T> T |
reduce(Collection<T> source,
@NotNull BinaryOperator<T> accumulator)
将集合从第一个开始进行累加操作,若集合为空或第所有元素为null则返回null。
|
static <T,R> R |
reduce(Collection<T> source,
@NotNull Function<T,R> mapper,
@NotNull BinaryOperator<R> accumulator)
将集合转为另一类型集合,并对转换后的集合元素从第一个非null元素开始进行累加操作,若集合为空或第所有元素为null则返回null。
|
static <T,R> R |
reduce(Collection<T> source,
@NotNull Function<T,R> mapper,
R first,
@NotNull BinaryOperator<R> accumulator)
将集合转为另一类型集合,并对转换后的集合元素进行累加操作,总是过滤转换源集合与转换后的集合中为null的元素
eg:
List<String> source = Arrays.asList("200", "300", null, "500");
BigDecimal result = reduce(source, BigDecimal::new, BigDecimal.ZERO, BigDecimal::add);
System.out.println(result); // 1000
|
static <T> T |
reduce(Collection<T> source,
T base,
@NotNull BinaryOperator<T> accumulator)
对集合元素进行累加操作,总是过滤集合中为null的元素
|
static <S,T,C extends Collection<T>> |
toCollection(@NotNull Collection<S> source,
@NotNull Supplier<C> collFactory,
@NotNull 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 <S,T,C extends Collection<T>> |
toCollection(@NotNull Collection<S> source,
@NotNull Supplier<C> collFactory,
@NotNull Supplier<T> itemFactory,
@NotNull BiConsumer<S,T> itemProcessor)
针对源集合中每一个元素创建另一类型元素,经过处理后返回另一类型元素祖册的集合,总是忽略源集合与返回集合中未null的元素
eg:
List<String> strings = Arrays.asList("200", "300", null, "500");
Set<List<String>> integers = toCollection(
strings,
HashSet::new, ArrayList::new,
(s, i) -> {
i.add(s);
i.add(s + s);
}
);
System.out.println(integers); // [[300, 300300], [500, 500500], [200, 200200]]
|
static <K,V,T,C extends Collection<T>> |
toCollection(Map<K,V> source,
@NotNull Supplier<C> collFactory,
@NotNull BiFunction<K,V,T> mapper)
将一个Map集合转为另一指定Collection集合, 总是过滤转换后的集合中为null的元素
eg: {@code Map |
static <S,T> @NotNull List<T> |
toList(Collection<S> source,
@NotNull Function<S,T> mapper)
将一个集合转为另一ArrayList集合
|
static <S,T> @NotNull List<T> |
toList(Collection<S> source,
@NotNull Supplier<T> itemFactory,
@NotNull BiConsumer<S,T> itemProcessor)
针对源集合中每一个元素创建另一类型元素,经过处理后返回另一类型元素的集合,总是忽略源集合与返回集合中未null的元素
eg:
List<String> strings = Arrays.asList("200", "300", null, "500");
List<List<String>> integers = toList(
strings,
ArrayList::new,
(s, i) -> {
i.add(s);
i.add(s + s);
}
);
System.out.println(integers); // [[300, 300300], [500, 500500], [200, 200200]]
|
static <T> @NotNull List<T> |
toList(Collection<T> source)
将一个集合转为ArrayList集合
|
static <K,V,T> @NotNull List<T> |
toList(Map<K,V> source,
@NotNull BiFunction<K,V,T> mapper)
将一个Map集合转为ArrayList集合, 总是过滤转换后的集合中为null的元素
|
static <T,K> @NotNull Map<K,T> |
toMap(Collection<T> source,
@NotNull Function<T,K> keyMapper)
将集合按指定条件映射为map集合
|
static <T,K,V> @NotNull Map<K,V> |
toMap(Collection<T> source,
@NotNull Function<T,K> keyMapper,
@NotNull Function<T,V> valueMapper)
将集合按指定条件映射为map集合
|
static <A,B,C,D> @NotNull Map<C,D> |
toMap(Map<A,B> source,
@NotNull Function<A,C> keyMapper,
@NotNull Function<B,D> valueMapper,
@NotNull Supplier<Map<C,D>> mapFactory)
将一种类型的Map集合转换为另一种类型的Map集合
eg: {@code Map |
static <S,T> @NotNull Set<T> |
toSet(Collection<S> source,
@NotNull Function<S,T> mapper)
将一个集合转为另一HashSet集合
|
static <T> @NotNull Set<T> |
toSet(Collection<T> source)
将一个集合转为HashSet集合
|
static <K,V,T> @NotNull Set<T> |
toSet(Map<K,V> source,
@NotNull BiFunction<K,V,T> mapper)
将一个集合转为另一HashSet集合
|
public static boolean isEmpty(Map<?,?> target)
public static boolean isNotEmpty(Map<?,?> target)
public static boolean isEmpty(Collection<?> target)
public static boolean isNotEmpty(Collection<?> target)
public static <T> T getFirst(Iterable<T> target)
@NotNull public static <T,K,V> @NotNull Map<K,V> toMap(Collection<T> source, @NotNull @NotNull Function<T,K> keyMapper, @NotNull @NotNull Function<T,V> valueMapper)
T - 源集合元素类型K - key类型V - value类型source - 源集合keyMapper - key生成方法valueMapper - value生成方法IllegalStateException - 当存在相同key时抛出@NotNull public static <T,K> @NotNull Map<K,T> toMap(Collection<T> source, @NotNull @NotNull Function<T,K> keyMapper)
T - 源集合元素类型K - key类型source - 源集合keyMapper - key生成方法IllegalStateException - 当存在相同key时抛出@NotNull public static <A,B,C,D> @NotNull Map<C,D> toMap(Map<A,B> source, @NotNull @NotNull Function<A,C> keyMapper, @NotNull @NotNull Function<B,D> valueMapper, @NotNull @NotNull Supplier<Map<C,D>> mapFactory)
Map<Integer, String> source = MapUtil.<Integer, String> builder()
.put(123, "123")
.put(233, "123")
.put(456, "456")
.build();
Map<BigDecimal, List<String>> result = toMap(source, BigDecimal::new, Collections::singletonList, HashMap::new);
System.out.println(result); // {233=[123], 123=[123], 456=[456]}
A - 源集合key类型B - 源集合value类型C - 目标集合key类型D - 目标集合value类型source - 源集合keyMapper - key映射方法valueMapper - value映射方法mapFactory - 指定Map集合的生成方法, 生成的集合不能为nullIllegalStateException - 当存在相同key时抛出public static <K1,V,K2> Map<K2,V> mapKeys(Map<K1,V> source, Function<K1,K2> keyMapper)
source - 源集合keyMapper - 映射方法public static <K,V1,V2> Map<K,V2> mapValues(Map<K,V1> source, Function<V1,V2> valueMapper)
source - 源集合valueMapper - 映射方法@NotNull public static <T,K,V,C extends Collection<V>> @NotNull Map<K,C> groupBy(Collection<T> source, @NotNull @NotNull Function<T,K> keyMapper, @NotNull @NotNull Function<T,V> valueMapper, @NotNull @NotNull Supplier<C> collFactory)
T - 源集合元素类型K - key类型V - 转换value集合元素类型C - 转换value集合类型source - 源集合keyMapper - 分组key映射方法valueMapper - 分组后value集合元素的映射方法collFactory - 指定集合的生成方法, 生成的集合不能为null@NotNull public static <T,K> @NotNull Map<K,List<T>> groupBy(Collection<T> source, @NotNull @NotNull Function<T,K> keyMapper)
T - 源集合元素类型K - key类型source - 源集合keyMapper - 分组key映射方法@NotNull public static <S,K,V,R> @NotNull Map<K,R> groupByAndThen(Collection<S> source, @NotNull @NotNull Function<S,K> classify, @NotNull @NotNull Function<S,V> valueMapper, @NotNull @NotNull BiFunction<K,List<V>,R> mergeMapper)
List source = Arrays.asList("123", "123", "233", "456");
Map result = toMapAndThen(
source,
Function.identity(),
Integer::valueOf,
(id, list) -> "\"id: " + id + ", count: " + list.size() + "\""
);
System.out.println(result); // {456="id: 456, count: 1", 233="id: 233, count: 1", 123="id: 123, count: 2"}
S - 源集合元素类型K - 分组后key类型V - val转换后集合的元素类型R - 转换后的val集合映射结果类型source - 目标classify - 分类方法valueMapper - 对分组结果的val集合的转换方法mergeMapper - 对转换后获得的集合的映射@NotNull public static <S,K,R> @NotNull Map<K,R> groupByAndThen(Collection<S> source, @NotNull @NotNull Function<S,K> classify, @NotNull @NotNull BiFunction<K,List<S>,R> mergeMapper)
List source = Arrays.asList("123", "123", "233", "456");
Map result = toMapAndThen(
source,
Function.identity(),
(id, list) -> "\"id: " + id + ", count: " + list.size() + "\""
);
System.out.println(result); // {456="id: 456, count: 1", 233="id: 233, count: 1", 123="id: 123, count: 2"}
S - 源集合元素类型K - 分组后key类型R - 转换后的val集合映射结果类型source - 目标classify - 分类方法mergeMapper - 对转换后获得的集合的映射@NotNull public static <S,T,K,R> @NotNull List<R> groupByThenFlat(List<S> source, @NotNull @NotNull Function<S,K> classify, @NotNull @NotNull Function<S,T> mapperAfterClassify, @NotNull @NotNull BiFunction<K,List<T>,R> flatMapper)
List<String> source = Arrays.asList("123", "156", "233", "244", "567");
List<String> result = groupByThenFlat(
source,
str -> str.substring(0, 1),
str -> "item: " + str,
(key, values) -> key + ": " + values.toString()
);
System.out.println(result); // 1: [item: 123, item: 156], 5: [item: 567], 2: [item: 233, item: 244]
S - 源集合元素类型K - 分组后key类型T - 返回集合元素类型R - 返回集合元素类型source - 源集合classify - 分组方法mapperAfterClassify - 分组后value映射方法flatMapper - 分组结果映射方法@NotNull public static <S,T,C extends Collection<T>> C toCollection(@NotNull @NotNull Collection<S> source, @NotNull @NotNull Supplier<C> collFactory, @NotNull @NotNull 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 - 映射方法@NotNull public static <S,T,C extends Collection<T>> C toCollection(@NotNull @NotNull Collection<S> source, @NotNull @NotNull Supplier<C> collFactory, @NotNull @NotNull Supplier<T> itemFactory, @NotNull @NotNull BiConsumer<S,T> itemProcessor)
List<String> strings = Arrays.asList("200", "300", null, "500");
Set<List<String>> integers = toCollection(
strings,
HashSet::new, ArrayList::new,
(s, i) -> {
i.add(s);
i.add(s + s);
}
);
System.out.println(integers); // [[300, 300300], [500, 500500], [200, 200200]]
S - 源集合元素类型T - 返回集合元素类型C - 返回集合类型source - 源集合collFactory - 指定集合的生成方法, 生成的集合不能为nullitemFactory - 另一类型元素生成方法itemProcessor - 源集合与返回集合的处理方法@NotNull public static <K,V,T,C extends Collection<T>> C toCollection(Map<K,V> source, @NotNull @NotNull Supplier<C> collFactory, @NotNull @NotNull BiFunction<K,V,T> mapper)
Map<Integer, String> source = MapUtil.<Integer, String> builder()
.put(123, "123")
.put(233, "123")
.put(456, "456")
.build();
Set<String> result = toCollection(source, HashSet::new, (k, v) -> k.toString() + v);
System.out.println(result); // [456456, 123123, 233123]
K - Map集合的key类型V - Map集合的value类型T - 返回集合元素类型C - 返回集合类型source - 源集合mapper - 映射方法collFactory - 指定集合的生成方法, 生成的集合不能为null@NotNull public static <S,T> @NotNull Set<T> toSet(Collection<S> source, @NotNull @NotNull Function<S,T> mapper)
S - 源集合元素类型T - 返回集合元素类型source - 源集合mapper - 映射方法@NotNull public static <T> @NotNull Set<T> toSet(Collection<T> source)
T - 集合元素类型source - 源集合@NotNull public static <K,V,T> @NotNull Set<T> toSet(Map<K,V> source, @NotNull @NotNull BiFunction<K,V,T> mapper)
K - 源集合key类型V - 源集合value类型T - 返回集合元素类型source - 源集合mapper - 映射方法@NotNull public static <K,V,T> @NotNull List<T> toList(Map<K,V> source, @NotNull @NotNull BiFunction<K,V,T> mapper)
K - Map集合的key类型V - Map集合的value类型T - 返回集合元素类型source - 源集合mapper - 映射方法@NotNull public static <S,T> @NotNull List<T> toList(Collection<S> source, @NotNull @NotNull Function<S,T> mapper)
S - 源集合元素类型T - 返回集合元素类型source - 源集合mapper - 映射方法@NotNull public static <T> @NotNull List<T> toList(Collection<T> source)
T - 返回集合元素类型source - 源集合@NotNull public static <S,T> @NotNull List<T> toList(Collection<S> source, @NotNull @NotNull Supplier<T> itemFactory, @NotNull @NotNull BiConsumer<S,T> itemProcessor)
List<String> strings = Arrays.asList("200", "300", null, "500");
List<List<String>> integers = toList(
strings,
ArrayList::new,
(s, i) -> {
i.add(s);
i.add(s + s);
}
);
System.out.println(integers); // [[300, 300300], [500, 500500], [200, 200200]]
S - 源集合元素类型T - 返回集合元素类型source - 源集合itemFactory - 另一类型元素生成方法itemProcessor - 源集合与返回集合的处理方法@NotNull public static <T> @NotNull List<T> filter(Collection<T> source, @NotNull @NotNull Predicate<T> filter)
T - 源集合元素类型source - 源集合filter - 过滤器@NotNull public static <K,V> @NotNull Map<K,V> filter(Map<K,V> source, @NotNull @NotNull BiPredicate<K,V> filter)
K - 源集合key类型V - 源集合value类型source - 源集合filter - 过滤器public static <T> List<T> filterNotNull(Collection<T> source)
T - 源集合元素类型source - 源集合@NotNull public static <K,V> @NotNull Map<K,V> filterByReverse(Map<K,V> source, @NotNull @NotNull BiPredicate<K,V> filter)
Map<Integer, String> source = MapUtil.<Integer, String> builder()
.put(123, "123")
.put(233, "123")
.put(456, "456")
.build();
Map<Integer, String> result = filterByReverse(source, (k, v) -> !k.toString().equals(v));
System.out.println(result); // {456=456, 123=123}
K - 源集合key类型V - 源集合value类型source - 源集合filter - 过滤器@NotNull public static <T> @NotNull List<T> filterByReverse(Collection<T> source, @NotNull @NotNull Predicate<T> filter)
List<String> source = Arrays.asList("123", "456", "789", "456");
List<String> result = filterByReverse(source, s -> s.equals("456"));
System.out.println(result); // [123, 789]
T - 源集合元素类型source - 源集合filter - 过滤器@NotNull public static <T,R> R reduce(Collection<T> source, @NotNull @NotNull Function<T,R> mapper, @NotNull R first, @NotNull @NotNull BinaryOperator<R> accumulator)
List<String> source = Arrays.asList("200", "300", null, "500");
BigDecimal result = reduce(source, BigDecimal::new, BigDecimal.ZERO, BigDecimal::add);
System.out.println(result); // 1000
T - 源集合元素类型R - 返回元素类型source - 源集合mapper - 映射方法first - 基类元素accumulator - 累加器public static <T,R> R reduce(Collection<T> source, @NotNull @NotNull Function<T,R> mapper, @NotNull @NotNull BinaryOperator<R> accumulator)
List<BigDecimal> source = Arrays.asList(null, new BigDecimal("200"), new BigDecimal("300"), null, new BigDecimal("500"));
BigDecimal result = reduce(source, Function.identity(), BigDecimal::add);
System.out.println(result); // 1000
T - 源集合元素类型R - 返回元素类型source - 源集合mapper - 映射方法accumulator - 累加器public static <T> T reduce(Collection<T> source, @NotNull @NotNull BinaryOperator<T> accumulator)
List<BigDecimal> source = Arrays.asList(null, new BigDecimal("200"), new BigDecimal("300"), null, new BigDecimal("500"));
BigDecimal result = reduce(source, BigDecimal::add);
System.out.println(result); // 1000
T - 源集合元素类型source - 源集合accumulator - 累加器reduce(Collection, Function, BinaryOperator)@NotNull public static <T> T reduce(Collection<T> source, @NotNull T base, @NotNull @NotNull BinaryOperator<T> accumulator)
T - 源集合元素类型source - 源集合base - 基类元素accumulator - 累加器public static <T,C extends Collection<T>> C foreach(C source, @NotNull @NotNull Consumer<T> action)
T - 源集合元素类型C - 源集合类型source - 源集合action - 操作public static <K,V,M extends Map<K,V>> M foreach(M source, @NotNull @NotNull BiConsumer<K,V> action)
K - 源集合元素类型V - 源集合类型source - 源集合action - 操作@NotNull public static <S,T,C extends Collection<T>> C flatMap(Collection<S> source, @NotNull @NotNull Function<S,? extends Collection<T>> mapper, @NotNull @NotNull Supplier<C> collFactory)
List<String> source = Arrays.asList("123", "421", null, "233");
Set<String> result = flatMap(source, s -> Arrays.asList(s.split("")), HashSet::new);
System.out.println(result); // [1, 2, 3, 4]
S - 源集合元素类型T - 平铺集合类型C - 返回集合类型source - 源集合mapper - 映射方法collFactory - 指定集合的生成方法, 生成的集合不能为null@NotNull public static <S,T,R extends Collection<T>> @NotNull List<T> flatMap(Collection<S> source, @NotNull @NotNull Function<S,R> mapper)
List<String> source = Arrays.asList("123", "421", null, "233");
List<String> result = flatMap(source, s -> Arrays.asList(s.split("")));
System.out.println(result); // [1, 2, 3, 4, 2, 1, 2, 3, 3]
S - 源集合元素类型T - 平铺集合类型source - 源集合mapper - 映射方法@NotNull public static <T> @NotNull String joining(Collection<T> source, Function<T,? extends CharSequence> charMapper, CharSequence delimiter, boolean filterBank)
List<Object> objects = Arrays.asList("", null, " ", 123, 456);
System.out.println(joining(objects, String::valueOf, ", ", true)); // 123, 456
System.out.println(joining(objects, String::valueOf, ", ", false)); // , , 123, 456
source - 源集合charMapper - 字符串映射方法delimiter - 分隔符filterBank - 是否过滤空白字符串,包括""与" "@NotNull public static <T> @NotNull String joining(Collection<T> source, Function<T,? extends CharSequence> charMapper)
List<Object> objects = Arrays.asList("", null, " ", 123, 456);
System.out.println(joining(objects, String::valueOf)); // 123456
source - 源集合charMapper - 字符串映射方法public static Collection<?> adaptToCollection(Object target)
Collections.singletonList(Object)target - 目标数据Copyright © 2022. All rights reserved.