1首先定义返回值类型,下面的数量是发货行逻辑,上面的各种id是各种维度,后续可以根据各种维度计算不同单据的数量
@Getter
@Setter
public class ShipmentOrderItemNumberModel {
/**
* id
*/
private Long id;
/**
* 租户id
*/
private Long tenantId;
/**
* 订单id
*/
private Long orderId;
/**
* 订单行id
*/
private Long orderItemId;
/**
* 发货单id
*/
private Long shipmentOrderId;
/**
* 交货计划行id
*/
private Long orderDeliveryPlanId;
/**
* 已发货数量
*/
private BigDecimal deliveredQuantity;
/**
* 在途数量
*/
private BigDecimal inTransitQuantity;
/**
* 到货数量
*/
private BigDecimal arrivedQuantity;
/**
* 收货数量
*/
private BigDecimal receivedQuantity;
/**
* 拒收数量
*/
private BigDecimal rejectQuantity;
/**
* 拒收可退数量
*/
private BigDecimal canReturnQuantity;
}
计算已发货单详情为维度的各种数量
java
public List<ShipmentOrderItemNumberModel> getQuantityByIds(List<Long> ids, Long tenantId) {
if (CollectionUtils.isEmpty(ids)) {
return null;
}
List<ShipmentOrderItemDO> shipmentOrderItemDOS = shipmentOrderItemRepository.listByIds(ids, tenantId);
Map<Long, ShipmentOrderItemDO> shipmentOrderItemDOMap = CollectionHelper.collectMap(shipmentOrderItemDOS, ShipmentOrderItemDO::getId);
// 过滤掉已作废
shipmentOrderItemDOS = CollectionHelper.filter(shipmentOrderItemDOS, shipmentOrderItemDO -> !ShipmentOrderStatusEnum.CANCEL.getCode().equals(shipmentOrderItemDO.getStatus()));
// 已发数量
Map<Long, BigDecimal> alreadyShipmentNumber = CollectionHelper.groupBigDecimalToSum(shipmentOrderItemDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, ShipmentOrderItemDO::getNumber);
// 在途数量
List<ShipmentOrderItemDO> inTransitDOS = CollectionHelper.filter(shipmentOrderItemDOS, shipmentOrderItemDO -> ShipmentOrderStatusEnum.IN_TRANSIT.getCode().equals(shipmentOrderItemDO.getStatus()));
Map<Long, BigDecimal> inTransitNumber = CollectionHelper.groupBigDecimalToSum(inTransitDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, ShipmentOrderItemDO::getNumber);
// 到货数量
Map<Long, BigDecimal> arrivedNumber = CollectionHelper.groupBigDecimalToSum(shipmentOrderItemDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, ShipmentOrderItemDO::getArriveNumber);
// 收货数量
Map<Long, BigDecimal> receivedNumber = CollectionHelper.groupBigDecimalToSum(shipmentOrderItemDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, ShipmentOrderItemDO::getReceiveNumber);
// 拒收数量
Map<Long, BigDecimal> rejectNumber = CollectionHelper.groupBigDecimalToSum(shipmentOrderItemDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, shipmentOrderItemDO ->{
String status = shipmentOrderItemDO.getStatus();
if (!ShipmentOrderStatusEnum.PART_RECEIVED.getCode().equals(status)) {
return BigDecimal.ZERO;
}
return shipmentOrderItemDO.getNumber().subtract(shipmentOrderItemDO.getReceiveNumber());
});
// 拒收可退货数量
List<ReturnOrderItemModel> returnOrderItemModels = returnOrderItemService.listByShipmentOrderItemIds(ids, tenantId);
returnOrderItemModels = CollectionHelper.filter(returnOrderItemModels, returnOrderItemModel -> !ReturnOrderStatusEnum.CANCEL.getCode().equals(returnOrderItemModel.getStatus()));
Map<Long, BigDecimal> returnOrderItemNumberGroup = CollectionHelper.groupBigDecimalToSum(returnOrderItemModels, ReturnOrderItemModel::getShipmentOrderItemId, ReturnOrderItemModel::getNumber);
Map<Long, BigDecimal> canReturnNumber = CollectionHelper.groupBigDecimalToSum(shipmentOrderItemDOS, ShipmentOrderItemDO::getOrderDeliveryPlanId, shipmentOrderItemDO ->{
BigDecimal returnNumber = MapHelper.getValue(returnOrderItemNumberGroup, shipmentOrderItemDO.getId());
String status = shipmentOrderItemDO.getStatus();
if (!ShipmentOrderStatusEnum.PART_RECEIVED.getCode().equals(status)) {
return BigDecimal.ZERO;
}
return shipmentOrderItemDO.getNumber().subtract(shipmentOrderItemDO.getReceiveNumber()).subtract(returnNumber);
});
return ShipmentOrderItemServiceConverter.toShipmentOrderItemNumberModel(ids, shipmentOrderItemDOMap, alreadyShipmentNumber, inTransitNumber, arrivedNumber, receivedNumber, rejectNumber, canReturnNumber, tenantId);
}
以发货计划行为例,根据不同维度,计算出单据要展示的数量
java
private void fillShipmentRelatedData(List<OrderDeliveryPlanModel> orderDeliveryPlanModelList, Long tenantId) {
List<Long> deliveryPlanIds = orderDeliveryPlanModelList.stream()
.map(OrderDeliveryPlanModel::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(deliveryPlanIds)) {
// 批量查询发货数量和收货数量
List<ShipmentOrderItemModel> shipmentOrderItemModelList =shipmentOrderItemService.listByDeliveryIdList(deliveryPlanIds, tenantId);
if (CollectionUtils.isEmpty(shipmentOrderItemModelList)) {
// 如果没有查询到发货数量和收货数量,则将发货计划订单数量填充到模型的未发货数量
orderDeliveryPlanModelList.forEach(model -> {
BigDecimal orderNumber = Objects.nonNull(model.getNumber()) ? model.getNumber() : BigDecimal.ZERO;
model.setUndeliveredQuantity(orderNumber);
});
return;
}
List<Long> shipmentOrderItemIdList= shipmentOrderItemModelList.stream()
.map(ShipmentOrderItemModel::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
List<ShipmentOrderItemNumberModel> shipmentOrderItemNumberModelList = shipmentOrderItemService.getQuantityByIds(shipmentOrderItemIdList, tenantId);
// 将查询到的数量填充到模型中
if (CollectionUtils.isEmpty(shipmentOrderItemNumberModelList)) {
// 如果没有查询到发货数量和收货数量,则将发货计划订单数量填充到模型的未发货数量
orderDeliveryPlanModelList.forEach(model -> {
BigDecimal orderNumber = Objects.nonNull(model.getNumber()) ? model.getNumber() : BigDecimal.ZERO;
model.setUndeliveredQuantity(orderNumber);
});
return;
}
Map<Long, BigDecimal> inTransitNumberMap= CollectionHelper.groupBigDecimalToSum(shipmentOrderItemNumberModelList, ShipmentOrderItemNumberModel::getOrderDeliveryPlanId, ShipmentOrderItemNumberModel::getInTransitQuantity);
Map<Long, BigDecimal> receivedNumberMap= CollectionHelper.groupBigDecimalToSum(shipmentOrderItemNumberModelList, ShipmentOrderItemNumberModel::getOrderDeliveryPlanId, ShipmentOrderItemNumberModel::getReceivedQuantity);
Map<Long, BigDecimal> arrivedNumberMap= CollectionHelper.groupBigDecimalToSum(shipmentOrderItemNumberModelList, ShipmentOrderItemNumberModel::getOrderDeliveryPlanId, ShipmentOrderItemNumberModel::getArrivedQuantity);
Map<Long, BigDecimal> deliveredNumberMap= CollectionHelper.groupBigDecimalToSum(shipmentOrderItemNumberModelList, ShipmentOrderItemNumberModel::getOrderDeliveryPlanId, ShipmentOrderItemNumberModel::getDeliveredQuantity);
orderDeliveryPlanModelList.forEach(model -> {
BigDecimal inTransitQuantity = Objects.nonNull(inTransitNumberMap.get(model.getId())) ?
inTransitNumberMap.get(model.getId()): BigDecimal.ZERO;
BigDecimal receivedQuantity = Objects.nonNull(receivedNumberMap.get(model.getId())) ?
receivedNumberMap.get(model.getId()): BigDecimal.ZERO;
BigDecimal arrivedQuantity = Objects.nonNull(arrivedNumberMap.get(model.getId())) ?
arrivedNumberMap.get(model.getId()): BigDecimal.ZERO;
BigDecimal deliveredQuantity =Objects.nonNull(deliveredNumberMap.get(model.getId())) ?
deliveredNumberMap.get(model.getId()): BigDecimal.ZERO;
model.setArrivedQuantity(arrivedQuantity);
model.setInTransitQuantity(inTransitQuantity);
model.setReceivedQuantity(receivedQuantity);
model.setDeliveredQuantity(deliveredQuantity);
// 未发货数量=订单数量-已发货数量
// 防止 model.getNumber() 为空指针
BigDecimal orderNumber = Objects.nonNull(model.getNumber()) ? model.getNumber() : BigDecimal.ZERO;
model.setUndeliveredQuantity(orderNumber.subtract(deliveredQuantity));
});
}
}
附件 CollectionHelper 工具类
java
package com.mdgyl.common.helper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mdgyl.common.constants.NumberConstants;
import com.mdgyl.common.helper.function.ToBigDecimalFunction;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author quanlin.shu
* @Description
* @date 2024/7/19
*/
public class CollectionHelper {
static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet();
@SuppressWarnings("unchecked")
private static <I, R> Function<I, R> castingIdentity() {
return i -> (R) i;
}
/**
* @author quanlin.shu
* @Description 转换集合
* @date 2024/7/19
*/
public static <E, R> List<R> collectList(Collection<E> collection, Function<? super E, ? extends R> mapper) {
return collectList(collection, mapper, Boolean.FALSE);
}
/**
* @author quanlin.shu
* @Description 转换集合 可以传入是否去重,默认不去重
* @date 2024/7/19
*/
public static <E, R> List<R> collectList(Collection<E> collection, Function<? super E, ? extends R> mapper, boolean distinct) {
if (CollectionUtils.isEmpty(collection)) {
return Lists.newArrayList();
}
Stream<R> stream = collection.stream().map(mapper);
if (distinct) {
stream = stream.distinct();
}
return stream.collect(Collectors.toList());
}
// public static <E, K, R> List<R> collectList(Collection<E> collection, Function<? super E, ? extends R> mapper1,
// Function<? super K, ? extends R> mapper2, boolean distinct) {
// if (CollectionUtils.isEmpty(collection)) {
// return Lists.newArrayList();
// }
// Stream<R> stream = collection.stream().map(mapper1).map(map::get).map(mapper2);
// if (distinct) {
// stream = stream.distinct();
// }
// return stream.collect(Collectors.toList());
// return null;
// }
/**
* @author quanlin.shu
* @Description 转换集合默认去重
* @date 2024/7/19
*/
public static <E, R> List<R> collectDistinctList(List<E> collection, Function<? super E, ? extends R> mapper) {
return collectNotNullList(collection, mapper, Boolean.TRUE);
}
/**
* @author quanlin.shu
* @Description 转换集合 并过滤为null对象
* @date 2024/7/19
*/
public static <E, R> List<R> collectNotNullList(List<E> collection, Function<? super E, ? extends R> mapper) {
return collectNotNullList(collection, mapper, Boolean.FALSE);
}
/**
* @author quanlin.shu
* @Description 转换集合 并过滤为null的对象,可以选择是否去重
* @date 2024/7/19
*/
public static <E, R> List<R> collectNotNullList(List<E> collection, Function<? super E, ? extends R> mapper,
boolean distinct) {
if (CollectionUtils.isEmpty(collection)) {
return null;
}
Stream<R> stream = collection.stream().map(mapper);
if (distinct) {
stream = stream.distinct();
}
return stream.filter(Objects::nonNull).collect(Collectors.toList());
}
/**
* @author quanlin.shu
* @Description 通过二维list,得到一个一维list
* @date 2024/7/19
*/
public static <E> List<E> collectFlatList(List<List<E>> collection) {
return collectFlatList(collection, Function.identity());
}
/**
* @author quanlin.shu
* @Description 通过二维list,得到一个一维list, 可以改变返回对象
* @date 2024/7/19
*/
public static <E, R> List<R> collectFlatList(List<List<E>> collection,
Function<? super E, ? extends R> mapper) {
if (CollectionUtils.isEmpty(collection)) {
return Lists.newArrayList();
}
return collection.stream().flatMap(Collection::stream).map(mapper).collect(Collectors.toList());
}
/**
* @author quanlin.shu
* @Description list转map
* @date 2024/7/19
*/
public static <E, K> Map<K, E> collectMap(List<E> collection,
Function<? super E, ? extends K> keyMapper) {
return collectMap(collection, keyMapper, Function.identity());
}
/**
* @author quanlin.shu
* @Description list转map, 可以选择想要的 key,value
* @date 2024/7/19
*/
public static <E, K, V> Map<K, V> collectMap(List<E> collection,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valMapper) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return filterCollectMap(collection, data -> Boolean.TRUE, keyMapper, valMapper);
}
/**
* @author quanlin.shu
* @Description list转map
* @date 2024/7/19
*/
public static <E, K> Map<K, E> filterCollectMap(List<E> collection,
Predicate<? super E> predicate,
Function<? super E, ? extends K> keyMapper) {
return filterCollectMap(collection, predicate, keyMapper, Function.identity());
}
/**
* @author quanlin.shu
* @Description list转map
* @date 2024/7/19
*/
public static <E, K, V> Map<K, V> filterCollectMap(List<E> collection,
Predicate<? super E> predicate,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valMapper) {
return collection.stream().filter(predicate).collect(Collectors.toMap(keyMapper, valMapper, (v1, v2) -> v2));
}
/**
* @author quanlin.shu
* @Description list转map, 过滤掉空的key-value
* @date 2024/7/19
*/
public static <E, K, V> Map<K, V> collectNotNullMap(List<E> collection,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valMapper) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return collection.stream().filter(e -> keyMapper.apply(e) != null && valMapper.apply(e) != null).collect(
Collectors.toMap(keyMapper, valMapper, (v1, v2) -> v2));
}
/**
* @author quanlin.shu
* @Description 根据条件过滤list
* @date 2024/7/19
*/
public static <E> List<E> filter(Collection<E> collection, Predicate<? super E> predicate) {
return filter(collection, predicate, true);
}
/**
* @author quanlin.shu
* @Description 根据条件过滤list, 可以选择是否去重(默认不去重)
* @date 2024/7/19
*/
public static <E> List<E> filter(Collection<E> collection, Predicate<? super E> predicate, boolean distinct) {
return filterCollectList(collection, predicate, Function.identity(), distinct);
}
public static <E, R> List<R> filterCollectList(Collection<E> collection, Predicate<? super E> predicate,
Function<? super E, ? extends R> mapper) {
return filterCollectList(collection, predicate, mapper, Boolean.FALSE);
}
public static <E, R> List<R> filterCollectList(Collection<E> collection, Predicate<? super E> predicate,
Function<? super E, ? extends R> mapper, boolean distinct) {
if (CollectionUtils.isEmpty(collection)) {
return Lists.newArrayList();
}
Stream<E> stream = collection.stream();
if (distinct) {
stream = stream.distinct();
}
return stream.filter(predicate).map(mapper).collect(Collectors.toList());
}
/**
* @author quanlin.shu
* @Description 对集合进行元素去重
* @date 2024/7/19
*/
public static <E> List<E> distinct(List<E> collection) {
if (CollectionUtils.isEmpty(collection)) {
return Lists.newArrayList();
}
return collection.stream().distinct().collect(Collectors.toList());
}
/**
* @author quanlin.shu
* @Description 对集合进行分组操作
* @date 2024/7/19
*/
public static <E, K> Map<K, List<E>> group(List<E> collection, Function<? super E, ? extends K> group) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return filterGroup(collection, data -> Boolean.TRUE, group);
}
/**
* @author quanlin.shu
* @Description 对集合进行分组操作
* @date 2024/7/19
*/
public static <E, K> Map<K, List<E>> filterGroup(List<E> collection, Predicate<? super E> predicate, Function<? super E, ? extends K> group) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return collection.stream().filter(predicate).collect(Collectors.groupingBy(group));
}
/**
* @author quanlin.shu
* @Description 对集合进行分组, 自定义条件key和value
* @date 2024/7/19
*/
public static <E, K, V> Map<K, V> group(List<E> collection,
Function<? super E, ? extends K> group,
Function<List<E>, ? extends V> mapper) {
return filterGroup(collection, data -> Boolean.TRUE, group, mapper);
}
/**
* @author quanlin.shu
* @Description 对集合进行分组, 自定义条件key和value
* @date 2024/7/19
*/
public static <E, K, V> Map<K, V> filterGroup(List<E> collection,
Predicate<? super E> predicate,
Function<? super E, ? extends K> group,
Function<List<E>, ? extends V> mapper) {
if (CollectionUtils.isEmpty(collection)) {
return null;
}
return collection.stream().filter(predicate)
.collect(Collectors.groupingBy(group)).entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, v -> mapper.apply(v.getValue())));
}
/**
* @author quanlin.shu
* @Description 对集合进行分组, 并得到分组之后value的数值的和
* @date 2024/7/19
*/
public static <E, K> Map<K, BigDecimal> groupBigDecimalToSum(List<E> collection,
Function<? super E, ? extends K> group,
ToBigDecimalFunction<? super E> mapper) {
return groupBigDecimalToSum(collection, data -> Boolean.TRUE, group, mapper);
}
/**
* @author quanlin.shu
* @Description 对集合进行分组, 根据条件过滤, 并得到分组之后value的数值的和
* @date 2024/7/19
*/
public static <E, K> Map<K, BigDecimal> groupBigDecimalToSum(List<E> collection,
Predicate<E> predicate,
Function<? super E, ? extends K> group,
ToBigDecimalFunction<? super E> mapper) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return collection.stream().filter(predicate).collect(
Collectors.groupingBy(group, summingBigDecimal(mapper)));
}
/**
* @author quanlin.shu
* @Description 根据key值将所有value求和得到一个map, 默认值为null
* @date 2024/7/19
*/
public static <E> BigDecimal mapBigDecimalToSum(List<E> collection, Function<? super E, BigDecimal> mapper) {
return mapBigDecimalToSum(collection, mapper, null);
}
/**
* @author quanlin.shu
* @Description 根据key值将所有value求和得到一个map, 可以自定义默认值
* @date 2024/7/19
*/
public static <E> BigDecimal mapBigDecimalToSum(List<E> collection, Function<? super E, BigDecimal> mapper, BigDecimal defaultValue) {
if (CollectionUtils.isEmpty(collection)) {
return defaultValue;
}
BigDecimal value = collection.stream().map(mapper).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
if (value == null) {
return defaultValue;
}
return value;
}
/**
* @author quanlin.shu
* @Description 判断集合是否包含元素
* @date 2024/7/19
*/
public static <E> boolean contains(List<E> collection, E element) {
if (CollectionUtils.isEmpty(collection)) {
return false;
}
return collection.contains(element);
}
/**
* @author quanlin.shu
* @Description 对集合进行切割
* @date 2024/7/19
*/
public static <E, R> List<R> partition(List<E> collection, int size, Function<? super List<E>, ? extends List<R>> mapper) {
return Lists.partition(collection, size)
.parallelStream().map(mapper)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
/**
* @author quanlin.shu
* @Description 计算a与b的差,得到a的差
* @date 2024/7/19
*/
public static <E> List<E> subtract(List<E> a, List<E> b) {
if (CollectionUtils.isEmpty(a)) {
return a;
}
if (CollectionUtils.isEmpty(b)) {
return a;
}
return ListUtils.subtract(a, b);
}
/**
* @author quanlin.shu
* @Description 计算a与b的交集部分
* @date 2024/7/19
*/
public static <E> List<E> intersection(List<E> a, List<E> b) {
if (CollectionUtils.isEmpty(a)) {
return null;
}
if (CollectionUtils.isEmpty(b)) {
return null;
}
return ListUtils.intersection(a, b);
}
/**
* Simple implementation class for {@code Collector}.
*
* @param <T> the type of elements to be collected
* @param <R> the type of the result
*/
static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
private final Supplier<A> supplier;
private final BiConsumer<A, T> accumulator;
private final BinaryOperator<A> combiner;
private final Function<A, R> finisher;
private final Set<Characteristics> characteristics;
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Function<A, R> finisher, Set<Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Set<Characteristics> characteristics) {
this(supplier, accumulator, combiner, castingIdentity(), characteristics);
}
@Override
public BiConsumer<A, T> accumulator() {
return accumulator;
}
@Override
public Supplier<A> supplier() {
return supplier;
}
@Override
public BinaryOperator<A> combiner() {
return combiner;
}
@Override
public Function<A, R> finisher() {
return finisher;
}
@Override
public Set<Characteristics> characteristics() {
return characteristics;
}
}
public static <E> Collector<E, ?, BigDecimal> summingBigDecimal(ToBigDecimalFunction<E> mapper) {
return new CollectorImpl<>(() -> new BigDecimal[1], (a, t) -> {
if (a[0] == null) {
a[0] = BigDecimal.ZERO;
}
a[0] = a[0].add(mapper.applyAsBigDecimal(t));
}, (a, b) -> {
a[0] = a[0].add(b[0]);
return a;
}, a -> a[0], CH_NOID);
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @return int
*/
public static <T> int mapToInt(List<T> dataList,
ToIntFunction<T> function) {
return filterMapToInt(dataList, function, Objects::nonNull);
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @param filterPredicate 过滤条件
* @return int
*/
public static <T> int filterMapToInt(List<T> dataList,
ToIntFunction<T> function,
Predicate<T> filterPredicate) {
if (CollectionUtils.isEmpty(dataList)) {
return NumberConstants.ZERO_I;
}
return dataList.stream()
.filter(filterPredicate)
.mapToInt(function).sum();
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @return long
*/
public static <T> long mapToLong(List<T> dataList,
ToLongFunction<T> function) {
return filterMapToLong(dataList, function, Objects::nonNull);
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @param filterPredicate 过滤条件
* @return long
*/
public static <T> long filterMapToLong(List<T> dataList,
ToLongFunction<T> function,
Predicate<T> filterPredicate) {
if (CollectionUtils.isEmpty(dataList)) {
return NumberConstants.ZERO_L;
}
return dataList.stream()
.filter(filterPredicate)
.mapToLong(function).sum();
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @return double
*/
public static <T> Double mapToDouble(List<T> dataList,
ToDoubleFunction<T> function) {
return filterMapToDouble(dataList, function, Objects::nonNull);
}
/**
* 获取List集合里某一列的总和
*
* @param dataList 集合参数
* @param function 求和的属性值
* @param filterPredicate 过滤条件
* @return long
*/
public static <T> double filterMapToDouble(List<T> dataList,
ToDoubleFunction<T> function,
Predicate<T> filterPredicate) {
if (CollectionUtils.isEmpty(dataList)) {
return NumberConstants.ZERO_I;
}
return dataList.stream()
.filter(filterPredicate)
.mapToDouble(function).sum();
}
/**
* 将字符串根据特定字符分割后,转换成特定类型集合
*
* @param dataStr 字符串入参
* @param regex 分割符号
* @param mapFunction 转换后类型函数
* @return List<R>
*/
public static <R> List<R> stringToList(String dataStr, String regex, Function<String, R> mapFunction) {
if (StringUtils.isBlank(dataStr)) {
return Lists.newArrayList();
}
return Arrays.stream(dataStr.split(regex))
.filter(Objects::nonNull)
.map(mapFunction)
.collect(Collectors.toList());
}
/**
* 将集合转换成字符串
*
* @param dataList 集合入参
* @param regex 分割符号
* @return String
*/
public static <R> String listToString(List<R> dataList,
String regex) {
if (CollectionUtils.isEmpty(dataList)) {
return null;
}
return dataList.stream().map(String::valueOf)
.collect(Collectors.joining(regex));
}
public static <T> int getCollectionSize(List<T> dataList) {
return CollectionUtils.isEmpty(dataList) ? NumberConstants.ZERO_I : dataList.size();
}
}