!676 【6.x】新增CollectorUtil.reduceListMap()用于Collection<Map<K,V>> => Map<K,List<V>>,优化groupingBy中对null的处理

Merge pull request !676 from 阿超/v6-dev
This commit is contained in:
Looly 2022-07-05 14:57:55 +00:00 committed by Gitee
commit 6f97c9ed42
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 80 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package cn.hutool.core.stream;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import java.util.Collections;
import java.util.EnumSet;
@ -9,6 +10,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
@ -104,7 +107,10 @@ public class CollectorUtil {
final BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
final K key = Opt.ofNullable(t).map(classifier).orElse(null);
final A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
if (ArrayUtil.isArray(container) || Objects.nonNull(t)) {
// 如果是数组类型不需要判空场景分组后需要使用java.util.stream.Collectors.counting 求null元素个数
downstreamAccumulator.accept(container, t);
}
};
final BinaryOperator<Map<K, A>> merger = mapMerger(downstream.combiner());
@SuppressWarnings("unchecked") final Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
@ -211,5 +217,39 @@ public class CollectorUtil {
};
}
/**
* 聚合这种数据类型:{@code Collection<Map<K,V>> => Map<K,List<V>>}
* 其中key相同的value会累加到List中
*
* @param <K> key的类型
* @param <V> value的类型
* @return 聚合后的map
*/
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
*/
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 -> {
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;
}
);
}
}

View File

@ -153,7 +153,7 @@ public class CollStreamUtilTest {
// 对null友好
final Map<Long, Map<Long, Student>> termIdClassIdStudentMap = CollStreamUtil.group2Map(Arrays.asList(null, new Student(2, 2, 1, "王五")), Student::getTermId, Student::getClassId);
final Map<Long, Map<Long, Student>> termIdClassIdStudentCompareMap = new HashMap<Long, Map<Long, Student>>() {{
put(null, MapUtil.of(null, null));
put(null, MapUtil.empty());
put(2L, MapUtil.of(2L, new Student(2, 2, 1, "王五")));
}};
Assert.assertEquals(termIdClassIdStudentCompareMap, termIdClassIdStudentMap);
@ -215,7 +215,7 @@ public class CollStreamUtilTest {
new Student(1, 2, 1, "李四"));
final Map<Long, List<Student>> termIdStudentsMap = CollStreamUtil.groupBy(students, Student::getTermId, Collectors.toList());
final Map<Long, List<Student>> termIdStudentsCompareMap = new HashMap<>();
termIdStudentsCompareMap.put(null, Arrays.asList(null, null));
termIdStudentsCompareMap.put(null, Collections.emptyList());
termIdStudentsCompareMap.put(1L, Arrays.asList(new Student(1L, 1, 1, "张三"), new Student(1L, 2, 1, "李四")));
Assert.assertEquals(termIdStudentsCompareMap, termIdStudentsMap);

View File

@ -0,0 +1,37 @@
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;
/**
* CollectorUtilTest
*
* @author VampireAchao
* @since 2022/7/3
*/
public class CollectorUtilTest {
@Test
public void reduceListMapTest() {
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());
// 执行聚合
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);
}
}