Blame view

src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java 3.25 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
7
  import cn.hutool.core.collection.CollectionUtil;
  
  import java.util.*;
  import java.util.function.Function;
cf93019c   YunaiV   增加 get-routers 接口...
8
  import java.util.function.Predicate;
0a607861   YunaiV   common 包,基础组件
9
10
11
12
13
14
15
16
17
  import java.util.stream.Collectors;
  
  /**
   * Collection 工具类
   *
   * @author 芋道源码
   */
  public class CollectionUtils {
  
823c26a5   YunaiV   批量提交一次迁移的代码
18
19
      public static boolean isAnyEmpty(Collection<?>... collections) {
          return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty);
0a607861   YunaiV   common 包,基础组件
20
21
      }
  
cf93019c   YunaiV   增加 get-routers 接口...
22
      public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
f942b34d   YunaiV   1. 完成用户导出的功能
23
24
25
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
cf93019c   YunaiV   增加 get-routers 接口...
26
27
28
          return from.stream().filter(predicate).collect(Collectors.toList());
      }
  
0a607861   YunaiV   common 包,基础组件
29
      public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
30
31
32
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
0a607861   YunaiV   common 包,基础组件
33
34
35
36
          return from.stream().map(func).collect(Collectors.toList());
      }
  
      public static <T, U> Set<U> convertSet(List<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
37
38
39
          if (CollUtil.isEmpty(from)) {
              return new HashSet<>();
          }
0a607861   YunaiV   common 包,基础组件
40
41
42
43
          return from.stream().map(func).collect(Collectors.toSet());
      }
  
      public static <T, K> Map<K, T> convertMap(List<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
44
45
46
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
47
48
49
50
          return from.stream().collect(Collectors.toMap(keyFunc, item -> item));
      }
  
      public static <T, K, V> Map<K, V> convertMap(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
51
52
53
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
54
55
56
57
          return from.stream().collect(Collectors.toMap(keyFunc, valueFunc));
      }
  
      public static <T, K> Map<K, List<T>> convertMultiMap(List<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
58
59
60
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
61
62
63
64
65
          return from.stream().collect(Collectors.groupingBy(keyFunc,
                  Collectors.mapping(t -> t, Collectors.toList())));
      }
  
      public static <T, K, V> Map<K, List<V>> convertMultiMap(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
66
67
68
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
69
70
71
72
73
74
          return from.stream().collect(Collectors.groupingBy(keyFunc,
                  Collectors.mapping(valueFunc, Collectors.toList())));
      }
  
      // 暂时没想好名字,先以 2 结尾噶
      public static <T, K, V> Map<K, Set<V>> convertMultiMap2(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
75
76
77
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
          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;
      }
  
      public static <T> void addIfNotNull(Collection<T> coll, T item) {
          if (item == null) {
              return;
          }
          coll.add(item);
      }
  
  }