Blame view

src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java 2.5 KB
0a607861   YunaiV   common 包,基础组件
1
2
3
4
5
6
  package cn.iocoder.dashboard.util.collection;
  
  import cn.hutool.core.collection.CollectionUtil;
  
  import java.util.*;
  import java.util.function.Function;
cf93019c   YunaiV   增加 get-routers 接口...
7
  import java.util.function.Predicate;
0a607861   YunaiV   common 包,基础组件
8
9
10
11
12
13
14
15
16
17
18
19
20
  import java.util.stream.Collectors;
  
  /**
   * Collection 工具类
   *
   * @author 芋道源码
   */
  public class CollectionUtils {
  
      public static <T> Set<T> asSet(T... objs) {
          return new HashSet<>(Arrays.asList(objs));
      }
  
cf93019c   YunaiV   增加 get-routers 接口...
21
22
23
24
      public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
          return from.stream().filter(predicate).collect(Collectors.toList());
      }
  
0a607861   YunaiV   common 包,基础组件
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
      public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
          return from.stream().map(func).collect(Collectors.toList());
      }
  
      public static <T, U> Set<U> convertSet(List<T> from, Function<T, U> func) {
          return from.stream().map(func).collect(Collectors.toSet());
      }
  
      public static <T, K> Map<K, T> convertMap(List<T> from, Function<T, K> keyFunc) {
          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) {
          return from.stream().collect(Collectors.toMap(keyFunc, valueFunc));
      }
  
      public static <T, K> Map<K, List<T>> convertMultiMap(List<T> from, Function<T, K> keyFunc) {
          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) {
          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) {
          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);
      }
  
  }