Blame view

src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java 3.64 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 {
  
f60855fa   YunaiV   1. 完成 Job 的 CRUD 功能
18
19
20
21
      public static boolean containsAny(Object source, Object... targets) {
          return Arrays.asList(targets).contains(source);
      }
  
823c26a5   YunaiV   批量提交一次迁移的代码
22
23
      public static boolean isAnyEmpty(Collection<?>... collections) {
          return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty);
0a607861   YunaiV   common 包,基础组件
24
25
      }
  
cf93019c   YunaiV   增加 get-routers 接口...
26
      public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
f942b34d   YunaiV   1. 完成用户导出的功能
27
28
29
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
cf93019c   YunaiV   增加 get-routers 接口...
30
31
32
          return from.stream().filter(predicate).collect(Collectors.toList());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
33
      public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
34
35
36
          if (CollUtil.isEmpty(from)) {
              return new ArrayList<>();
          }
0a607861   YunaiV   common 包,基础组件
37
38
39
          return from.stream().map(func).collect(Collectors.toList());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
40
      public static <T, U> Set<U> convertSet(Collection<T> from, Function<T, U> func) {
f942b34d   YunaiV   1. 完成用户导出的功能
41
42
43
          if (CollUtil.isEmpty(from)) {
              return new HashSet<>();
          }
0a607861   YunaiV   common 包,基础组件
44
45
46
          return from.stream().map(func).collect(Collectors.toSet());
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
47
      public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
48
49
50
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
51
52
53
          return from.stream().collect(Collectors.toMap(keyFunc, item -> item));
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
54
      public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
55
56
57
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
58
59
60
          return from.stream().collect(Collectors.toMap(keyFunc, valueFunc));
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
61
      public static <T, K> Map<K, List<T>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
62
63
64
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
65
66
67
68
          return from.stream().collect(Collectors.groupingBy(keyFunc,
                  Collectors.mapping(t -> t, Collectors.toList())));
      }
  
753c7678   YunaiV   完成主要在线 session 的功能
69
      public static <T, K, V> Map<K, List<V>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
70
71
72
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
73
74
75
76
77
          return from.stream().collect(Collectors.groupingBy(keyFunc,
                  Collectors.mapping(valueFunc, Collectors.toList())));
      }
  
      // 暂时没想好名字,先以 2 结尾噶
753c7678   YunaiV   完成主要在线 session 的功能
78
      public static <T, K, V> Map<K, Set<V>> convertMultiMap2(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
f942b34d   YunaiV   1. 完成用户导出的功能
79
80
81
          if (CollUtil.isEmpty(from)) {
              return new HashMap<>();
          }
0a607861   YunaiV   common 包,基础组件
82
83
84
85
86
87
88
89
90
91
92
          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 模板
93
94
95
96
97
98
99
      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 包,基础组件
100
101
102
103
104
105
106
107
      public static <T> void addIfNotNull(Collection<T> coll, T item) {
          if (item == null) {
              return;
          }
          coll.add(item);
      }
  
  }