Commit 8a9c714586c221cfab0db76a4ea8929f4bca9e94
Committed by
Gitee
!27 CollectionUtils 新增distinct、convertMap 重载函数
Merge pull request !27 from 贺小五/feature/coll
Showing
1 changed file
with
49 additions
and
6 deletions
src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java
| @@ -4,8 +4,10 @@ import cn.hutool.core.collection.CollUtil; | @@ -4,8 +4,10 @@ import cn.hutool.core.collection.CollUtil; | ||
| 4 | import cn.hutool.core.collection.CollectionUtil; | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 5 | 5 | ||
| 6 | import java.util.*; | 6 | import java.util.*; |
| 7 | +import java.util.function.BinaryOperator; | ||
| 7 | import java.util.function.Function; | 8 | import java.util.function.Function; |
| 8 | import java.util.function.Predicate; | 9 | import java.util.function.Predicate; |
| 10 | +import java.util.function.Supplier; | ||
| 9 | import java.util.stream.Collectors; | 11 | import java.util.stream.Collectors; |
| 10 | 12 | ||
| 11 | /** | 13 | /** |
| @@ -30,6 +32,20 @@ public class CollectionUtils { | @@ -30,6 +32,20 @@ public class CollectionUtils { | ||
| 30 | return from.stream().filter(predicate).collect(Collectors.toList()); | 32 | return from.stream().filter(predicate).collect(Collectors.toList()); |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 35 | + public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper) { | ||
| 36 | + if (CollUtil.isEmpty(from)) { | ||
| 37 | + return new ArrayList<>(); | ||
| 38 | + } | ||
| 39 | + return distinct(from, keyMapper, (t1, t2) -> t1); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper, BinaryOperator<T> cover) { | ||
| 43 | + if (CollUtil.isEmpty(from)) { | ||
| 44 | + return new ArrayList<>(); | ||
| 45 | + } | ||
| 46 | + return new ArrayList<>(convertMap(from, keyMapper, Function.identity(), cover).values()); | ||
| 47 | + } | ||
| 48 | + | ||
| 33 | public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) { | 49 | public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) { |
| 34 | if (CollUtil.isEmpty(from)) { | 50 | if (CollUtil.isEmpty(from)) { |
| 35 | return new ArrayList<>(); | 51 | return new ArrayList<>(); |
| @@ -48,30 +64,57 @@ public class CollectionUtils { | @@ -48,30 +64,57 @@ public class CollectionUtils { | ||
| 48 | if (CollUtil.isEmpty(from)) { | 64 | if (CollUtil.isEmpty(from)) { |
| 49 | return new HashMap<>(); | 65 | return new HashMap<>(); |
| 50 | } | 66 | } |
| 51 | - return from.stream().collect(Collectors.toMap(keyFunc, item -> item)); | 67 | + return convertMap(from, keyFunc, Function.identity()); |
| 68 | + } | ||
| 69 | + | ||
| 70 | + public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc, Supplier<? extends Map<K, T>> supplier) { | ||
| 71 | + if (CollUtil.isEmpty(from)) { | ||
| 72 | + return supplier.get(); | ||
| 73 | + } | ||
| 74 | + return convertMap(from, keyFunc, Function.identity(), supplier); | ||
| 52 | } | 75 | } |
| 53 | 76 | ||
| 54 | public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) { | 77 | public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) { |
| 55 | if (CollUtil.isEmpty(from)) { | 78 | if (CollUtil.isEmpty(from)) { |
| 56 | return new HashMap<>(); | 79 | return new HashMap<>(); |
| 57 | } | 80 | } |
| 58 | - return from.stream().collect(Collectors.toMap(keyFunc, valueFunc)); | 81 | + return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1); |
| 82 | + } | ||
| 83 | + | ||
| 84 | + public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc, BinaryOperator<V> mergeFunction) { | ||
| 85 | + if (CollUtil.isEmpty(from)) { | ||
| 86 | + return new HashMap<>(); | ||
| 87 | + } | ||
| 88 | + return convertMap(from, keyFunc, valueFunc, mergeFunction, HashMap::new); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc, Supplier<? extends Map<K, V>> supplier) { | ||
| 92 | + if (CollUtil.isEmpty(from)) { | ||
| 93 | + return supplier.get(); | ||
| 94 | + } | ||
| 95 | + return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1, supplier); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc, BinaryOperator<V> mergeFunction, Supplier<? extends Map<K, V>> supplier) { | ||
| 99 | + if (CollUtil.isEmpty(from)) { | ||
| 100 | + return new HashMap<>(); | ||
| 101 | + } | ||
| 102 | + return from.stream().collect(Collectors.toMap(keyFunc, valueFunc, mergeFunction, supplier)); | ||
| 59 | } | 103 | } |
| 60 | 104 | ||
| 61 | public static <T, K> Map<K, List<T>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc) { | 105 | public static <T, K> Map<K, List<T>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc) { |
| 62 | if (CollUtil.isEmpty(from)) { | 106 | if (CollUtil.isEmpty(from)) { |
| 63 | return new HashMap<>(); | 107 | return new HashMap<>(); |
| 64 | } | 108 | } |
| 65 | - return from.stream().collect(Collectors.groupingBy(keyFunc, | ||
| 66 | - Collectors.mapping(t -> t, Collectors.toList()))); | 109 | + return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList()))); |
| 67 | } | 110 | } |
| 68 | 111 | ||
| 69 | public static <T, K, V> Map<K, List<V>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) { | 112 | public static <T, K, V> Map<K, List<V>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) { |
| 70 | if (CollUtil.isEmpty(from)) { | 113 | if (CollUtil.isEmpty(from)) { |
| 71 | return new HashMap<>(); | 114 | return new HashMap<>(); |
| 72 | } | 115 | } |
| 73 | - return from.stream().collect(Collectors.groupingBy(keyFunc, | ||
| 74 | - Collectors.mapping(valueFunc, Collectors.toList()))); | 116 | + return from.stream() |
| 117 | + .collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList()))); | ||
| 75 | } | 118 | } |
| 76 | 119 | ||
| 77 | // 暂时没想好名字,先以 2 结尾噶 | 120 | // 暂时没想好名字,先以 2 结尾噶 |