mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
针对审查意见修改
This commit is contained in:
parent
dc0d9591c9
commit
bdf538a932
@ -55,7 +55,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return toMap(collection, (v) -> Opt.ofNullable(v).map(key).get(), Function.identity(), isParallel);
|
||||
}
|
||||
@ -88,7 +88,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(HashMap::new, (m, v) -> m.put(key.apply(v), value.apply(v)), HashMap::putAll);
|
||||
@ -122,7 +122,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return groupBy(collection, key, Collectors.toList(), isParallel);
|
||||
}
|
||||
@ -160,7 +160,7 @@ public class CollStreamUtil {
|
||||
public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1,
|
||||
Function<E, U> key2, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return groupBy(collection, key1, CollectorUtil.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
}
|
||||
@ -197,7 +197,7 @@ public class CollStreamUtil {
|
||||
public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection,
|
||||
Function<E, T> key1, Function<E, U> key2, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return groupBy(collection, key1, CollectorUtil.toMap(key2, Function.identity(), (l, r) -> l), isParallel);
|
||||
}
|
||||
@ -235,7 +235,7 @@ public class CollStreamUtil {
|
||||
public static <E, K, V> Map<K, List<V>> groupKeyValue(Collection<E> collection, Function<E, K> key,
|
||||
Function<E, V> value, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return groupBy(collection, key, Collectors.mapping(v -> Opt.ofNullable(v).map(value).orElse(null), Collectors.toList()), isParallel);
|
||||
}
|
||||
@ -254,7 +254,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
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 MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return groupBy(collection, key, downstream, false);
|
||||
}
|
||||
@ -275,7 +275,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
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 MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel).collect(CollectorUtil.groupingBy(key, downstream));
|
||||
}
|
||||
@ -365,11 +365,11 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <K, X, Y, V> Map<K, V> merge(Map<K, X> map1, Map<K, Y> map2, BiFunction<X, Y, V> merge) {
|
||||
if (MapUtil.isEmpty(map1) && MapUtil.isEmpty(map2)) {
|
||||
return MapUtil.newHashMap();
|
||||
return MapUtil.newHashMap(0);
|
||||
} else if (MapUtil.isEmpty(map1)) {
|
||||
map1 = MapUtil.newHashMap();
|
||||
map1 = MapUtil.newHashMap(0);
|
||||
} else if (MapUtil.isEmpty(map2)) {
|
||||
map2 = MapUtil.newHashMap();
|
||||
map2 = MapUtil.newHashMap(0);
|
||||
}
|
||||
Set<K> key = new HashSet<>();
|
||||
key.addAll(map1.keySet());
|
||||
@ -386,57 +386,4 @@ public class CollStreamUtil {
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection过滤
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param function 过滤方法
|
||||
* @return 过滤后的list
|
||||
*/
|
||||
public static <E> List<E> filter(Collection<E> collection, Predicate<E> function) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return CollUtil.newArrayList();
|
||||
}
|
||||
return collection.stream().filter(function).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection拼接
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param function 拼接方法
|
||||
* @return 拼接后的list
|
||||
*/
|
||||
public static <E> String join(Collection<E> collection, Function<E, String> function) {
|
||||
return join(collection, function, ",");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection拼接
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param function 拼接方法
|
||||
* @param delimiter 拼接符
|
||||
* @return 拼接后的list
|
||||
*/
|
||||
public static <E> String join(Collection<E> collection, Function<E, String> function, CharSequence delimiter) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection排序
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param comparing 排序方法
|
||||
* @return 排序后的list
|
||||
*/
|
||||
public static <E> List<E> sorted(Collection<E> collection, Comparator<E> comparing) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return CollUtil.newArrayList();
|
||||
}
|
||||
return collection.stream().sorted(comparing).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user