Blame view

src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java 5.53 KB
0a607861   YunaiV   common 包,基础组件
1
2
  package cn.iocoder.dashboard.util.collection;
  
f942b34d   YunaiV   1. 完成用户导出的功能
3
  import cn.hutool.core.collection.CollUtil;
0a607861   YunaiV   common 包,基础组件
4
5
6
  import cn.hutool.core.collection.CollectionUtil;
  
  import java.util.*;
fc3810e7   hexiaowu   新增自定义对象的去重
7
  import java.util.function.BinaryOperator;
0a607861   YunaiV   common 包,基础组件
8
  import java.util.function.Function;
cf93019c   YunaiV   增加 get-routers 接口...
9
  import java.util.function.Predicate;
fc3810e7   hexiaowu   新增自定义对象的去重
10
  import java.util.function.Supplier;
0a607861   YunaiV   common 包,基础组件
11
12
13
14
15
16
17
18
19
  import java.util.stream.Collectors;
  
  /**
   * Collection 工具类
   *
   * @author 芋道源码
   */
  public class CollectionUtils {
  
f60855fa   YunaiV   1. 完成 Job 的 CRUD 功能
20
21
22
23
      public static boolean containsAny(Object source, Object... targets) {
          return Arrays.asList(targets).contains(source);
      }
  
823c26a5   YunaiV   批量提交一次迁移的代码
24
25
      public static boolean isAnyEmpty(Collection<?>... collections) {
          return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty);
0a607861   YunaiV   common 包,基础组件
26
27
      }
  
cf93019c   YunaiV   增加 get-routers 接口...
28
      public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
f942b34d   YunaiV   1. 完成用户导出的功能
29
30
31
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
cf93019c   YunaiV   增加 get-routers 接口...
32
33
34
          return from.stream().filter(predicate).collect(Collectors.toList());
      }
  
fc3810e7   hexiaowu   新增自定义对象的去重
35
36
37
38
39
40
41
42
43
44
45
46
47
48
      public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper) {
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
          return distinct(from, keyMapper, (t1, t2) -> t1);
      }
  
      public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper, BinaryOperator<T> cover) {
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
          return new ArrayList<>(convertMap(from, keyMapper, Function.identity(), cover).values());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
49
      public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
50
51
52
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
0a607861   YunaiV   common 包,基础组件
53
54
55
          return from.stream().map(func).collect(Collectors.toList());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
56
      public static <T, U> Set<U> convertSet(Collection<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
57
58
59
          if (CollUtil.isEmpty(from)) {
              return new HashSet<>();
          }
0a607861   YunaiV   common 包,基础组件
60
61
62
          return from.stream().map(func).collect(Collectors.toSet());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
63
      public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
64
65
66
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
fc3810e7   hexiaowu   新增自定义对象的去重
67
68
69
70
71
72
73
74
          return convertMap(from, keyFunc, Function.identity());
      }
  
      public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc, Supplier<? extends Map<K, T>> supplier) {
          if (CollUtil.isEmpty(from)) {
              return supplier.get();
          }
          return convertMap(from, keyFunc, Function.identity(), supplier);
0a607861   YunaiV   common 包,基础组件
75
76
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
77
      public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
78
79
80
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
fc3810e7   hexiaowu   新增自定义对象的去重
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
          return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1);
      }
  
      public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc, BinaryOperator<V> mergeFunction) {
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
          return convertMap(from, keyFunc, valueFunc, mergeFunction, HashMap::new);
      }
  
      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) {
          if (CollUtil.isEmpty(from)) {
              return supplier.get();
          }
          return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1, supplier);
      }
  
      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) {
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
          return from.stream().collect(Collectors.toMap(keyFunc, valueFunc, mergeFunction, supplier));
0a607861   YunaiV   common 包,基础组件
103
104
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
105
      public static <T, K> Map<K, List<T>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
106
107
108
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
fc3810e7   hexiaowu   新增自定义对象的去重
109
          return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList())));
0a607861   YunaiV   common 包,基础组件
110
111
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
112
      public static <T, K, V> Map<K, List<V>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
113
114
115
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
fc3810e7   hexiaowu   新增自定义对象的去重
116
117
          return from.stream()
                     .collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList())));
0a607861   YunaiV   common 包,基础组件
118
119
120
      }
  
      // 暂时没想好名字,先以 2 结尾噶
753c7678   YunaiV   完成主要在线 session 的功能
121
      public static <T, K, V> Map<K, Set<V>> convertMultiMap2(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
122
123
124
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
125
126
127
128
129
130
131
132
133
134
135
          return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toSet())));
      }
  
      public static boolean containsAny(Collection<?> source, Collection<?> candidates) {
          return org.springframework.util.CollectionUtils.containsAny(source, candidates);
      }
  
      public static <T> T getFirst(List<T> from) {
          return !CollectionUtil.isEmpty(from) ? from.get(0) : null;
      }
  
2210ed5c   YunaiV   增加 service 模板
136
137
138
139
140
141
142
      public static <T> T findFirst(List<T> from, Predicate<T> predicate) {
          if (CollUtil.isEmpty(from)) {
              return null;
          }
          return from.stream().filter(predicate).findFirst().orElse(null);
      }
  
0a607861   YunaiV   common 包,基础组件
143
144
145
146
147
148
149
150
      public static <T> void addIfNotNull(Collection<T> coll, T item) {
          if (item == null) {
              return;
          }
          coll.add(item);
      }
  
  }