mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
fc6a0567b4
commit
34835939d2
@ -11,6 +11,7 @@
|
|||||||
* 【core 】 合成注解SyntheticAnnotation提取为接口,并为实现类添加注解选择器和属性处理器(pr#678@Gitee)
|
* 【core 】 合成注解SyntheticAnnotation提取为接口,并为实现类添加注解选择器和属性处理器(pr#678@Gitee)
|
||||||
* 【core 】 增加BeanValueProvider(issue#I5FBHV@Gitee)
|
* 【core 】 增加BeanValueProvider(issue#I5FBHV@Gitee)
|
||||||
* 【core 】 Convert工具类中,新增中文大写数字金额转换为数字工具方法(pr#674@Gitee)
|
* 【core 】 Convert工具类中,新增中文大写数字金额转换为数字工具方法(pr#674@Gitee)
|
||||||
|
* 【core 】 新增CollectorUtil.reduceListMap()(pr#676@Gitee)
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
|
||||||
|
@ -3,12 +3,13 @@ package cn.hutool.core.stream;
|
|||||||
import cn.hutool.core.lang.Opt;
|
import cn.hutool.core.lang.Opt;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.List;
|
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.BinaryOperator;
|
import java.util.function.BinaryOperator;
|
||||||
@ -99,26 +100,23 @@ public class CollectorUtil {
|
|||||||
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
|
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
|
||||||
Supplier<M> mapFactory,
|
Supplier<M> mapFactory,
|
||||||
Collector<? super T, A, D> downstream) {
|
Collector<? super T, A, D> downstream) {
|
||||||
Supplier<A> downstreamSupplier = downstream.supplier();
|
final Supplier<A> downstreamSupplier = downstream.supplier();
|
||||||
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
|
final BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
|
||||||
BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
|
final BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
|
||||||
K key = Opt.ofNullable(t).map(classifier).orElse(null);
|
final K key = Opt.ofNullable(t).map(classifier).orElse(null);
|
||||||
A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
|
final A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
|
||||||
downstreamAccumulator.accept(container, t);
|
downstreamAccumulator.accept(container, t);
|
||||||
};
|
};
|
||||||
BinaryOperator<Map<K, A>> merger = mapMerger(downstream.combiner());
|
final BinaryOperator<Map<K, A>> merger = mapMerger(downstream.combiner());
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked") final Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
|
||||||
Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
|
|
||||||
|
|
||||||
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||||
return new SimpleCollector<>(mangledFactory, accumulator, merger, CH_ID);
|
return new SimpleCollector<>(mangledFactory, accumulator, merger, CH_ID);
|
||||||
} else {
|
} else {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked") final Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
|
||||||
Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
|
final Function<Map<K, A>, M> finisher = intermediate -> {
|
||||||
Function<Map<K, A>, M> finisher = intermediate -> {
|
|
||||||
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
|
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked") final M castResult = (M) intermediate;
|
||||||
M castResult = (M) intermediate;
|
|
||||||
return castResult;
|
return castResult;
|
||||||
};
|
};
|
||||||
return new SimpleCollector<>(mangledFactory, accumulator, merger, finisher, CH_NOID);
|
return new SimpleCollector<>(mangledFactory, accumulator, merger, finisher, CH_NOID);
|
||||||
@ -214,5 +212,40 @@ public class CollectorUtil {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 聚合这种数据类型:{@code Collection<Map<K,V>> => Map<K,List<V>>}
|
||||||
|
* 其中key相同的value,会累加到List中
|
||||||
|
*
|
||||||
|
* @param <K> key的类型
|
||||||
|
* @param <V> value的类型
|
||||||
|
* @return 聚合后的map
|
||||||
|
* @since 5.8.5
|
||||||
|
*/
|
||||||
|
public static <K, V> Collector<Map<K, V>, ?, Map<K, List<V>>> reduceListMap() {
|
||||||
|
return reduceListMap(HashMap::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 聚合这种数据类型:{@code Collection<Map<K,V>> => Map<K,List<V>>}
|
||||||
|
* 其中key相同的value,会累加到List中
|
||||||
|
*
|
||||||
|
* @param mapSupplier 可自定义map的类型如concurrentHashMap等
|
||||||
|
* @param <K> key的类型
|
||||||
|
* @param <V> value的类型
|
||||||
|
* @param <R> 返回值的类型
|
||||||
|
* @return 聚合后的map
|
||||||
|
* @since 5.8.5
|
||||||
|
*/
|
||||||
|
public static <K, V, R extends Map<K, List<V>>> Collector<Map<K, V>, ?, R> reduceListMap(final Supplier<R> mapSupplier) {
|
||||||
|
return Collectors.reducing(mapSupplier.get(), value -> {
|
||||||
|
final R result = mapSupplier.get();
|
||||||
|
value.forEach((k, v) -> result.computeIfAbsent(k, i -> new ArrayList<>()).add(v));
|
||||||
|
return result;
|
||||||
|
}, (l, r) -> {
|
||||||
|
r.forEach((k, v) -> l.computeIfAbsent(k, i -> new ArrayList<>()).addAll(v));
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class CollectorUtilTest {
|
||||||
|
@Test
|
||||||
|
public void reduceListMapTest() {
|
||||||
|
final Set<Map<String, Integer>> nameScoreMapList = StreamUtil.of(
|
||||||
|
// 集合内的第一个map,包含两个key value
|
||||||
|
MapUtil.builder("苏格拉底", 1).put("特拉叙马霍斯", 3).build(),
|
||||||
|
MapUtil.of("苏格拉底", 2),
|
||||||
|
MapUtil.of("特拉叙马霍斯", 1),
|
||||||
|
MapUtil.of("特拉叙马霍斯", 2)
|
||||||
|
).collect(Collectors.toSet());
|
||||||
|
// 执行聚合
|
||||||
|
final Map<String, List<Integer>> nameScoresMap = nameScoreMapList.stream().collect(CollectorUtil.reduceListMap());
|
||||||
|
|
||||||
|
Assert.assertEquals(MapUtil.builder("苏格拉底", Arrays.asList(1, 2))
|
||||||
|
.put("特拉叙马霍斯", Arrays.asList(3, 1, 2)).build(),
|
||||||
|
nameScoresMap);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user