|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package org.dromara.common.core.utils;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
|
|
|
|
|
@ -148,7 +149,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key) {
|
|
|
|
|
if (CollUtil.isEmpty(collection)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return collection.stream()
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
@ -169,7 +170,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value) {
|
|
|
|
|
if (CollUtil.isEmpty(collection)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return collection.stream()
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
@ -187,7 +188,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <K, E, V> Map<K, V> toMap(Map<K, E> map, BiFunction<K, E, V> take) {
|
|
|
|
|
if (CollUtil.isEmpty(map)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return toMap(map.entrySet(), Map.Entry::getKey, entry -> take.apply(entry.getKey(), entry.getValue()));
|
|
|
|
|
}
|
|
|
|
|
@ -204,7 +205,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key) {
|
|
|
|
|
if (CollUtil.isEmpty(collection)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return collection.stream()
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
@ -225,7 +226,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1, Function<E, U> key2) {
|
|
|
|
|
if (CollUtil.isEmpty(collection)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return collection.stream()
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
@ -246,7 +247,7 @@ public class StreamUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection, Function<E, T> key1, Function<E, U> key2) {
|
|
|
|
|
if (CollUtil.isEmpty(collection)) {
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
}
|
|
|
|
|
return collection.stream()
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
@ -310,7 +311,7 @@ public class StreamUtils {
|
|
|
|
|
public static <K, X, Y, V> Map<K, V> merge(Map<K, X> map1, Map<K, Y> map2, BiFunction<X, Y, V> merge) {
|
|
|
|
|
if (CollUtil.isEmpty(map1) && CollUtil.isEmpty(map2)) {
|
|
|
|
|
// 如果两个 map 都为空,则直接返回空的 map
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|
return MapUtil.newHashMap();
|
|
|
|
|
} else if (CollUtil.isEmpty(map1)) {
|
|
|
|
|
// 如果 map1 为空,则直接处理返回 map2
|
|
|
|
|
return toMap(map2.entrySet(), Map.Entry::getKey, entry -> merge.apply(null, entry.getValue()));
|
|
|
|
|
|