mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
e2710ae79e
commit
9768a4d080
@ -12,6 +12,7 @@
|
||||
* 【log 】 log4j2的编译依赖改为api,core为test依赖(pr#2019@Github)
|
||||
* 【core 】 Img.scale缩小默认使用平滑模式,增加scale方法重载可选模式(issue#I4MY6X@Gitee)
|
||||
* 【core 】 excel添加写入图片的方法(pr#486@Gitee)
|
||||
* 【core 】 增加CollStreamUtil.groupBy(pr#484@Gitee)
|
||||
*
|
||||
### 🐞Bug修复
|
||||
* 【core 】 LineReadWatcher#onModify文件清空判断问题(issue#2013@Github)
|
||||
|
@ -5,7 +5,14 @@ import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.stream.StreamUtil;
|
||||
|
||||
import java.util.*;
|
||||
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.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collector;
|
||||
@ -106,7 +113,7 @@ public class CollStreamUtil {
|
||||
* <B>{@code Collection<E> -------> Map<K,List<E>> } </B>
|
||||
*
|
||||
* @param collection 需要分组的集合
|
||||
* @param key 分组的规则
|
||||
* @param key 键分组的规则
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
@ -116,7 +123,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupThen(collection, key, Collectors.toList(), isParallel);
|
||||
return groupBy(collection, key, Collectors.toList(), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,7 +161,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupThen(collection, key1, Collectors.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
return groupBy(collection, key1, Collectors.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -191,7 +198,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupThen(collection, key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l), isParallel);
|
||||
return groupBy(collection, key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -199,8 +206,8 @@ public class CollStreamUtil {
|
||||
* <B>{@code Collection<E> -------> Map<K,List<V>> } </B>
|
||||
*
|
||||
* @param collection 需要分组的集合
|
||||
* @param key 分组的规则
|
||||
* @param value 分组的规则
|
||||
* @param key 键分组的规则
|
||||
* @param value 值分组的规则
|
||||
* @param <E> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
* @param <V> List中的value类型
|
||||
@ -216,8 +223,8 @@ public class CollStreamUtil {
|
||||
* <B>{@code Collection<E> -------> Map<K,List<V>> } </B>
|
||||
*
|
||||
* @param collection 需要分组的集合
|
||||
* @param key 分组的规则
|
||||
* @param value 分组的规则
|
||||
* @param key 键分组的规则
|
||||
* @param value 值分组的规则
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
@ -229,7 +236,7 @@ public class CollStreamUtil {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupThen(collection, key, Collectors.mapping(value, Collectors.toList()), isParallel);
|
||||
return groupBy(collection, key, Collectors.mapping(value, Collectors.toList()), isParallel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -242,12 +249,13 @@ public class CollStreamUtil {
|
||||
* @param <K> map中的key类型
|
||||
* @param <D> 后续操作的返回值
|
||||
* @return 分组后的map
|
||||
* @since 5.7.18
|
||||
*/
|
||||
public static <E, K, D> Map<K, D> groupThen(Collection<E> collection, Function<E, K> key, Collector<E, ?, D> downstream) {
|
||||
public static <E, K, D> Map<K, D> groupBy(Collection<E> collection, Function<E, K> key, Collector<E, ?, D> downstream) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return groupThen(collection, key, downstream, false);
|
||||
return groupBy(collection, key, downstream, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -261,8 +269,10 @@ public class CollStreamUtil {
|
||||
* @param <K> map中的key类型
|
||||
* @param <D> 后续操作的返回值
|
||||
* @return 分组后的map
|
||||
* @see Collectors#groupingBy(Function, Collector)
|
||||
* @since 5.7.18
|
||||
*/
|
||||
public static <E, K, D> Map<K, D> groupThen(Collection<E> collection, Function<E, K> key, Collector<E, ?, D> downstream, boolean isParallel) {
|
||||
public static <E, K, D> Map<K, D> groupBy(Collection<E> collection, Function<E, K> key, Collector<E, ?, D> downstream, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -173,16 +172,16 @@ public class CollStreamUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupThen() {
|
||||
// groupThen作为之前所有group函数的公共部分抽取出来,并更接近于jdk原生,灵活性更强
|
||||
public void testGroupBy() {
|
||||
// groupBy作为之前所有group函数的公共部分抽取出来,并更接近于jdk原生,灵活性更强
|
||||
|
||||
// 参数null测试
|
||||
Map<Long, List<Student>> map = CollStreamUtil.groupThen(null, Student::getTermId, Collectors.toList());
|
||||
Map<Long, List<Student>> map = CollStreamUtil.groupBy(null, Student::getTermId, Collectors.toList());
|
||||
Assert.assertEquals(map, Collections.EMPTY_MAP);
|
||||
|
||||
// 参数空数组测试
|
||||
List<Student> list = new ArrayList<>();
|
||||
map = CollStreamUtil.groupThen(list, Student::getTermId, Collectors.toList());
|
||||
map = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.toList());
|
||||
Assert.assertEquals(map, Collections.EMPTY_MAP);
|
||||
|
||||
// 放入元素
|
||||
@ -190,11 +189,12 @@ public class CollStreamUtilTest {
|
||||
list.add(new Student(1, 2, 1, "李四"));
|
||||
list.add(new Student(2, 2, 1, "王五"));
|
||||
// 先根据termId分组,再通过classId比较,找出最大值所属的那个Student,返回的Optional
|
||||
Map<Long, Optional<Student>> longOptionalMap = CollStreamUtil.groupThen(list, Student::getTermId, Collectors.maxBy(Comparator.comparing(Student::getClassId)));
|
||||
Map<Long, Optional<Student>> longOptionalMap = CollStreamUtil.groupBy(list, Student::getTermId, Collectors.maxBy(Comparator.comparing(Student::getClassId)));
|
||||
//noinspection OptionalGetWithoutIsPresent
|
||||
Assert.assertEquals("李四", longOptionalMap.get(1L).get().getName());
|
||||
|
||||
// 先根据termId分组,再转换为Map<studentId,name>
|
||||
Map<Long, HashMap<Long, String>> groupThen = CollStreamUtil.groupThen(list, Student::getTermId, Collector.of(HashMap::new, (m, v) -> m.put(v.getStudentId(), v.getName()), (l, r) -> l));
|
||||
Map<Long, HashMap<Long, String>> groupThen = CollStreamUtil.groupBy(list, Student::getTermId, Collector.of(HashMap::new, (m, v) -> m.put(v.getStudentId(), v.getName()), (l, r) -> l));
|
||||
Assert.assertEquals(
|
||||
MapUtil.builder()
|
||||
.put(1L, MapUtil.builder().put(1L, "李四").build())
|
||||
|
Loading…
x
Reference in New Issue
Block a user