package org.sxkj.common.func; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.springframework.util.Assert; import java.lang.RuntimeException; import java.util.*; import java.util.function.*; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class Streams { public static Stream stream(List elements) { return CollectionUtils.isNotEmpty(elements) ? elements.stream() : Stream.empty(); } public static T first(Collection elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).findFirst().orElse(null); } return null; } public static Stream stream(List elements, boolean parallel) { if (CollectionUtils.isNotEmpty(elements)) { return StreamSupport.stream(elements.spliterator(), parallel); } return Stream.empty(); } public static long countBy(List elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).count(); } return 0; } public static List toList(T[] elements, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (ArrayUtils.isNotEmpty(elements)) { return Arrays.stream(elements).map(mapper).collect(Collectors.toList()); } return Lists.newArrayList(); } public static Set toSet(Set elements, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().map(mapper).collect(Collectors.toSet()); } return Sets.newHashSet(); } public static List forEach(List elements, Consumer consumer) { Assert.notNull(consumer, "consumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { elements.forEach(consumer); return elements; } return Lists.newArrayList(); } public static void forEach(Collection elements, Consumer consumer) { Assert.notNull(consumer, "consumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { elements.forEach(consumer); } } public static List forEach(List elements, Predicate predicate, Consumer consumer) { Assert.notNull(consumer, "consumer cannot be not empty!"); Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { elements.stream().filter(predicate).forEach(consumer); return elements; } return Lists.newArrayList(); } public static Set toSet(List elements, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().map(mapper).collect(Collectors.toSet()); } return Sets.newHashSet(); } public static List filter(List elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).collect(Collectors.toList()); } return Lists.newArrayList(); } public static R evalAs(Map map, K key, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (MapUtils.isNotEmpty(map) && map.containsKey(key)) { return Optional.of(map.get(key)).map(mapper).orElse(null); } return null; } public static IPage toPage(IPage page, Consumer> consumer) { Assert.notNull(consumer, "consumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(page.getRecords())) { consumer.accept(page.getRecords()); } return page; } public static IPage toPage(IPage page, Function mapper) { Assert.notNull(mapper, "function cannot be not empty!"); if (CollectionUtils.isNotEmpty(page.getRecords())) { List records = page.getRecords(); Object collect = records.stream() .map(mapper) .collect(Collectors.toList()); page.setRecords((List) collect); } return page; } public static List toList(Collection elements, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().map(mapper).collect(Collectors.toList()); } return Lists.newArrayList(); } public static boolean anyMatch(Collection elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().anyMatch(predicate); } return false; } public static void doContinue(Collection elements, Predicate predicate, Consumer> consumer) { Assert.notNull(predicate, "predicate cannot be not empty!"); Assert.notNull(consumer, "consumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements) && elements.stream().anyMatch(predicate)) { consumer.accept(elements.stream().filter(predicate).collect(Collectors.toList())); } } public static boolean allMatch(Collection elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().allMatch(predicate); } return false; } public static boolean noneMatch(Collection elements, Predicate predicate) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().noneMatch(predicate); } return false; } public static void ifPresent(Map map, K key, Consumer consumer) { Assert.notNull(consumer, "consumer cannot be not empty!"); if (MapUtils.isNotEmpty(map) && map.containsKey(key)) { Optional.ofNullable(map.get(key)).ifPresent(consumer); } } public static void nonAccept(List elements, Consumer> recordConsumer) { Assert.notNull(recordConsumer, "recordConsumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { recordConsumer.accept(elements); } } public static R reduce(List elements, R identity, BinaryOperator accumulator) { Assert.notNull(identity, "identity cannot be not empty!"); Assert.notNull(accumulator, "accumulator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().reduce(identity, accumulator); } return identity; } public static Map> groupBy(List elements, Function classifier) { Assert.notNull(classifier, "classifier cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().collect(Collectors.groupingBy(classifier)); } return Maps.newHashMap(); } public static Set toSet(Map map, Function, R> mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (map != null && !map.isEmpty()) { return map.entrySet().stream().map(mapper).collect(Collectors.toSet()); } return Sets.newHashSet(); } public static List toList(Map map, Function, R> mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); if (MapUtils.isNotEmpty(map)) { return map.entrySet().stream().map(mapper).collect(Collectors.toList()); } return Lists.newArrayList(); } public static void ifPresent(Collection elements, Predicate predicate, Consumer consumer) { Assert.notNull(predicate, "predicate cannot be not empty!"); Assert.notNull(consumer, "consumer cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { elements.stream().filter(predicate).findFirst().ifPresent(consumer); } } public static Set toSet(List elements, Predicate predicate, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).map(mapper).collect(Collectors.toSet()); } return Sets.newHashSet(); } public static Set toSet(Map map, Predicate> predicate, Function, R> mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(predicate, "predicate cannot be not empty!"); if (map != null && !map.isEmpty()) { return map.entrySet().stream().filter(predicate).map(mapper).collect(Collectors.toSet()); } return Sets.newHashSet(); } public static T firstOrDefault(List elements, Predicate predicate, Supplier supplier) { Assert.notNull(predicate, "predicate cannot be not empty!"); Assert.notNull(supplier, "supplier cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).findFirst().orElseGet(supplier); } return supplier.get(); } public static R evalAs(Map map, K key, Function mapper, R defaultValue) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(defaultValue, "defaultValue cannot be not empty!"); if (MapUtils.isNotEmpty(map) && map.containsKey(key)) { return Optional.of(map.get(key)).map(mapper).orElse(defaultValue); } return defaultValue; } public static R evalAs(Map map, K key, Function mapper, Supplier supplier) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(supplier, "supplier cannot be not empty!"); if (MapUtils.isNotEmpty(map) && map.containsKey(key)) { return Optional.of(map.get(key)).map(mapper).orElseGet(supplier); } return supplier.get(); } public static List toList(List elements, Predicate predicate, Function mapper) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).map(mapper).collect(Collectors.toList()); } return Lists.newArrayList(); } public static List toSorted(List elements, Comparator comparator) { Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().sorted(comparator).collect(Collectors.toList()); } return Lists.newArrayList(); } public static List toSorted(List elements, Function mapper, Comparator comparator) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().map(mapper).sorted(comparator).collect(Collectors.toList()); } return Lists.newArrayList(); } public static void ifPresent(Map> map, K key, Predicate predicate, Consumer consumer) { Assert.notNull(predicate, "predicate cannot be not empty!"); Assert.notNull(consumer, "consumer cannot be not empty!"); if (MapUtils.isNotEmpty(map) && map.containsKey(key)) { map.get(key).stream().filter(predicate).findFirst().ifPresent(consumer); } } public static Map toMap(List elements, Function keyMapper, Function valueMapper) { if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null) { return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper)); } return Maps.newHashMap(); } public static Map toMap(T[] elements, Function keyMapper, Function valueMapper) { if (ArrayUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null) { return Arrays.stream(elements).collect(Collectors.toMap(keyMapper, valueMapper)); } return Maps.newHashMap(); } public static > Map toMap(List elements, Function keyMapper, Function valueMapper, BinaryOperator mergeFunction) { if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null && mergeFunction != null) { return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction)); } return Maps.newHashMap(); } public static > Map toMap(List elements, Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier) { if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null && mergeFunction != null && mapSupplier != null) { return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier)); } return Maps.newLinkedHashMap(); } public static Map> groupBy(List elements, Predicate predicate, Function classifier) { if (CollectionUtils.isNotEmpty(elements) && predicate != null && classifier != null) { return elements.stream().filter(predicate).collect(Collectors.groupingBy(classifier)); } return Maps.newHashMap(); } /** * 分组后计算总数 */ public static Map groupByCount(List elements, Function mapper) { if (CollectionUtils.isNotEmpty(elements) && mapper != null) { return elements.stream().map(mapper).collect(Collectors.groupingBy( e -> e, Collectors.counting() )); } return Maps.newHashMap(); } public static R reduce(List elements, Function mapper, R identity, BinaryOperator accumulator) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(identity, "identity cannot be not empty!"); Assert.notNull(accumulator, "accumulator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().map(mapper).reduce(identity, accumulator); } return identity; } public static R reduce(List elements, Predicate predicate, Function mapper, R identity, BinaryOperator accumulator) { Assert.notNull(mapper, "mapper cannot be not empty!"); Assert.notNull(identity, "identity cannot be not empty!"); Assert.notNull(predicate, "predicate cannot be not empty!"); Assert.notNull(accumulator, "accumulator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).map(mapper).reduce(identity, accumulator); } return identity; } public static T minOrThrow(List elements, Comparator comparator, String exMessage) { Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().min(comparator).orElseThrow(() -> new RuntimeException(exMessage)); } throw new RuntimeException("elements cannot be not empty!"); } public static T maxOrThrow(List elements, Comparator comparator, String exMessage) { Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().max(comparator).orElseThrow(() -> new java.lang.RuntimeException(exMessage)); } throw new RuntimeException("elements cannot be not empty!"); } public static T firstOrThrow(List elements, Predicate predicate, String exMessage) { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).findFirst().orElseThrow(() -> new RuntimeException(exMessage)); } throw new RuntimeException("elements cannot be not empty!"); } public static T firstOrThrow(List elements, Predicate predicate, Supplier exceptionSupplier) throws X { Assert.notNull(predicate, "predicate cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().filter(predicate).findFirst().orElseThrow(exceptionSupplier); } throw new RuntimeException("elements cannot be not empty!"); } public static T minOrThrow(List elements, Comparator comparator, Supplier exceptionSupplier) throws X { Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().min(comparator).orElseThrow(exceptionSupplier); } throw new RuntimeException("elements cannot be not empty!"); } public static T maxOrThrow(List elements, Comparator comparator, Supplier exceptionSupplier) throws X { Assert.notNull(comparator, "comparator cannot be not empty!"); if (CollectionUtils.isNotEmpty(elements)) { return elements.stream().max(comparator).orElseThrow(exceptionSupplier); } throw new RuntimeException("elements cannot be not empty!"); } public static Map toMap(List elements, Predicate predicate, Function keyMapper, Function valueMapper) { if (CollectionUtils.isNotEmpty(elements) && predicate != null && keyMapper != null && valueMapper != null) { return elements.stream().filter(predicate).collect(Collectors.toMap(keyMapper, valueMapper)); } return Maps.newHashMap(); } public static void doPartition(Collection collection, Consumer> consumer, int stepSize) { Assert.notEmpty(collection, "collection cannot be not empty"); List> partitionList = Lists.partition(Lists.newArrayList(collection), stepSize); for (List partition : partitionList) { consumer.accept(partition); } } public static void doWhile(Function, List> recordPageFunction, Consumer> recordConsumer, long batchSize) { Assert.notNull(recordConsumer, "recordConsumer cannot be not empty!"); Assert.notNull(recordPageFunction, "recordPageFunction cannot be not empty!"); Page page; long current = 1; List batchRecords; boolean hasNextPage = false; long size = batchSize > 0 ? batchSize : 10; do { page = Page.of(current, size, Boolean.FALSE); batchRecords = recordPageFunction.apply(page); if (CollectionUtils.isNotEmpty(batchRecords)) { recordConsumer.accept(batchRecords); hasNextPage = batchRecords.size() >= size; } current++; } while (CollectionUtils.isNotEmpty(batchRecords) && hasNextPage); } /** * 数据进行平铺 * * @param collections 二维数组平铺 * @param 类型 * @return 一维数据 */ public static List flatMap(List> collections) { if (CollectionUtils.isEmpty(collections)) { return Lists.newArrayList(); } return collections.stream() .filter(Objects::nonNull) .flatMap(List::stream) .filter(Objects::nonNull) .collect(Collectors.toList()); } }