CollectionUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package cn.iocoder.dashboard.util.collection;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import java.util.*;
  5. import java.util.function.BinaryOperator;
  6. import java.util.function.Function;
  7. import java.util.function.Predicate;
  8. import java.util.function.Supplier;
  9. import java.util.stream.Collectors;
  10. /**
  11. * Collection 工具类
  12. *
  13. * @author 芋道源码
  14. */
  15. public class CollectionUtils {
  16. public static boolean containsAny(Object source, Object... targets) {
  17. return Arrays.asList(targets).contains(source);
  18. }
  19. public static boolean isAnyEmpty(Collection<?>... collections) {
  20. return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty);
  21. }
  22. public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
  23. if (CollUtil.isEmpty(from)) {
  24. return new ArrayList<>();
  25. }
  26. return from.stream().filter(predicate).collect(Collectors.toList());
  27. }
  28. public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper) {
  29. if (CollUtil.isEmpty(from)) {
  30. return new ArrayList<>();
  31. }
  32. return distinct(from, keyMapper, (t1, t2) -> t1);
  33. }
  34. public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper, BinaryOperator<T> cover) {
  35. if (CollUtil.isEmpty(from)) {
  36. return new ArrayList<>();
  37. }
  38. return new ArrayList<>(convertMap(from, keyMapper, Function.identity(), cover).values());
  39. }
  40. public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) {
  41. if (CollUtil.isEmpty(from)) {
  42. return new ArrayList<>();
  43. }
  44. return from.stream().map(func).collect(Collectors.toList());
  45. }
  46. public static <T, U> Set<U> convertSet(Collection<T> from, Function<T, U> func) {
  47. if (CollUtil.isEmpty(from)) {
  48. return new HashSet<>();
  49. }
  50. return from.stream().map(func).collect(Collectors.toSet());
  51. }
  52. public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc) {
  53. if (CollUtil.isEmpty(from)) {
  54. return new HashMap<>();
  55. }
  56. return convertMap(from, keyFunc, Function.identity());
  57. }
  58. public static <T, K> Map<K, T> convertMap(Collection<T> from, Function<T, K> keyFunc, Supplier<? extends Map<K, T>> supplier) {
  59. if (CollUtil.isEmpty(from)) {
  60. return supplier.get();
  61. }
  62. return convertMap(from, keyFunc, Function.identity(), supplier);
  63. }
  64. public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
  65. if (CollUtil.isEmpty(from)) {
  66. return new HashMap<>();
  67. }
  68. return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1);
  69. }
  70. public static <T, K, V> Map<K, V> convertMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc, BinaryOperator<V> mergeFunction) {
  71. if (CollUtil.isEmpty(from)) {
  72. return new HashMap<>();
  73. }
  74. return convertMap(from, keyFunc, valueFunc, mergeFunction, HashMap::new);
  75. }
  76. 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) {
  77. if (CollUtil.isEmpty(from)) {
  78. return supplier.get();
  79. }
  80. return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1, supplier);
  81. }
  82. 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) {
  83. if (CollUtil.isEmpty(from)) {
  84. return new HashMap<>();
  85. }
  86. return from.stream().collect(Collectors.toMap(keyFunc, valueFunc, mergeFunction, supplier));
  87. }
  88. public static <T, K> Map<K, List<T>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc) {
  89. if (CollUtil.isEmpty(from)) {
  90. return new HashMap<>();
  91. }
  92. return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList())));
  93. }
  94. public static <T, K, V> Map<K, List<V>> convertMultiMap(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
  95. if (CollUtil.isEmpty(from)) {
  96. return new HashMap<>();
  97. }
  98. return from.stream()
  99. .collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList())));
  100. }
  101. // 暂时没想好名字,先以 2 结尾噶
  102. public static <T, K, V> Map<K, Set<V>> convertMultiMap2(Collection<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
  103. if (CollUtil.isEmpty(from)) {
  104. return new HashMap<>();
  105. }
  106. return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toSet())));
  107. }
  108. public static boolean containsAny(Collection<?> source, Collection<?> candidates) {
  109. return org.springframework.util.CollectionUtils.containsAny(source, candidates);
  110. }
  111. public static <T> T getFirst(List<T> from) {
  112. return !CollectionUtil.isEmpty(from) ? from.get(0) : null;
  113. }
  114. public static <T> T findFirst(List<T> from, Predicate<T> predicate) {
  115. if (CollUtil.isEmpty(from)) {
  116. return null;
  117. }
  118. return from.stream().filter(predicate).findFirst().orElse(null);
  119. }
  120. public static <T> void addIfNotNull(Collection<T> coll, T item) {
  121. if (item == null) {
  122. return;
  123. }
  124. coll.add(item);
  125. }
  126. }