新增CollectorUtil.reduceListMap(),用于聚合集合中的 每一个map 为一个大map Collection<Map<K,V>> => Map<K,List<V>>

This commit is contained in:
achao 2022-07-03 19:05:57 +08:00 committed by VampireAchao
parent fec797aa86
commit 26cf1ba18f
2 changed files with 72 additions and 0 deletions

View File

@ -10,6 +10,7 @@ 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;
@ -216,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

@ -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);
}
}