mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge branch 'v5-dev' of gitee.com:dromara/hutool into v5-dev
This commit is contained in:
commit
8d33235eef
@ -3,16 +3,10 @@ package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.stream.CollectorUtil;
|
||||
import cn.hutool.core.stream.StreamUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collector;
|
||||
@ -161,7 +155,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupBy(collection, key1, Collectors.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
return groupBy(collection, key1, CollectorUtil.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -236,7 +230,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupBy(collection, key, Collectors.mapping(value, Collectors.toList()), isParallel);
|
||||
return groupBy(collection, key, Collectors.mapping(v -> Opt.ofNullable(v).map(value).orElse(null), Collectors.toList()), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -276,7 +270,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel).collect(Collectors.groupingBy(key, downstream));
|
||||
return StreamUtil.of(collection, isParallel).collect(CollectorUtil.groupingBy(key, downstream));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,9 +2,11 @@ package cn.hutool.core.stream;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
|
||||
/**
|
||||
@ -61,4 +63,70 @@ public class CollectorUtil {
|
||||
Collections.emptySet()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 提供对null值友好的groupingBy操作的{@link Collector}实现,可指定map类型
|
||||
*
|
||||
* @param classifier 分组依据
|
||||
* @param mapFactory 提供的map
|
||||
* @param downstream 下游操作
|
||||
* @param <T> 实体类型
|
||||
* @param <K> 实体中的分组依据对应类型,也是Map中key的类型
|
||||
* @param <D> 下游操作对应返回类型,也是Map中value的类型
|
||||
* @param <A> 下游操作在进行中间操作时对应类型
|
||||
* @param <M> 最后返回结果Map类型
|
||||
* @return {@link Collector}
|
||||
*/
|
||||
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
|
||||
Supplier<M> mapFactory,
|
||||
Collector<? super T, A, D> downstream) {
|
||||
Supplier<A> downstreamSupplier = downstream.supplier();
|
||||
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
|
||||
BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
|
||||
K key = Optional.ofNullable(t).map(classifier).orElse(null);
|
||||
A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
|
||||
downstreamAccumulator.accept(container, t);
|
||||
};
|
||||
BinaryOperator<Map<K, A>> merger = (m1, m2) -> {
|
||||
for (Map.Entry<K, A> e : m2.entrySet()) {
|
||||
m1.merge(e.getKey(), e.getValue(), downstream.combiner());
|
||||
}
|
||||
return m1;
|
||||
};
|
||||
@SuppressWarnings("unchecked")
|
||||
Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
|
||||
|
||||
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
return new SimpleCollector<>(mangledFactory, accumulator, merger, Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH)));
|
||||
} else {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
|
||||
Function<Map<K, A>, M> finisher = intermediate -> {
|
||||
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
|
||||
@SuppressWarnings("unchecked")
|
||||
M castResult = (M) intermediate;
|
||||
return castResult;
|
||||
};
|
||||
return new SimpleCollector<>(mangledFactory, accumulator, merger, finisher, Collections.emptySet());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供对null值友好的groupingBy操作的{@link Collector}实现
|
||||
*
|
||||
* @param classifier 分组依据
|
||||
* @param downstream 下游操作
|
||||
* @param <T> 实体类型
|
||||
* @param <K> 实体中的分组依据对应类型,也是Map中key的类型
|
||||
* @param <D> 下游操作对应返回类型,也是Map中value的类型
|
||||
* @param <A> 下游操作在进行中间操作时对应类型
|
||||
* @return {@link Collector}
|
||||
*/
|
||||
public static <T, K, A, D>
|
||||
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
|
||||
Collector<? super T, A, D> downstream) {
|
||||
return groupingBy(classifier, HashMap::new, downstream);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user