几种常见的工具类

反射工具类

package com.mdgyl.common.util;


import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * description: 反射工具类
 * author: wuyc
 * createTime: 2023-12-02 14:10:19
 */
public class ReflectionUtils {

    private static final String setStr = "set";

    private ReflectionUtils() {

    }

    /**
     * 设置对象中的属性值
     *
     * @param obj       需要设置的对象
     * @param fieldName 属性名
     * @param value     属性值
     */
    public static void setFieldValue(Object obj, String fieldName, Object value) {
        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(Boolean.TRUE);
            field.set(obj, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行Bean中的set方法 (mybatis 设置)
     *
     * @param object        bean对象
     * @param propertyName  bean中属性名
     * @param propertyValue bean中属性值
     */
    public static void invokeSetterMethod(Object object, String propertyName, Object propertyValue) {
        if (propertyValue == null) {
            return;
        }

        // 获取属性值的类型
        Class valueType = propertyValue.getClass();
        String methodName = setStr + StringUtils.capitalize(propertyName);

        Method setMethod = org.springframework.util.ReflectionUtils.findMethod(object.getClass(), methodName, valueType);
        if (Objects.nonNull(setMethod)) {
            org.springframework.util.ReflectionUtils.invokeMethod(setMethod, object, propertyValue);
        }
    }

    /**
     * 判断属性是否存在
     *
     * @param object    Bean对象
     * @param fieldName 属性名
     * @return 存在:true 不存在false
     */
    public static boolean existsField(Object object, String fieldName) {
        try {
            return object.getClass().getDeclaredField(fieldName) != null;
        } catch (NoSuchFieldException e) {
            return Boolean.FALSE;
        }
    }

    /**
     * 获取对象中指定字段的值
     *
     * @param obj       需要获取值的对象
     * @param fieldName 字段名
     * @return 字段的值,如果字段不存在或发生异常则返回 null
     */
    public static Object getFieldValue(Object obj, String fieldName) {
        try {
            // 获取指定名称的字段
            Field field = obj.getClass().getDeclaredField(fieldName);
            // 设置字段可访问
            field.setAccessible(true);
            // 返回字段的值
            return field.get(obj);
        } catch (NoSuchFieldException e) {
            // 字段不存在
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // 访问被拒绝
            e.printStackTrace();
        }
        // 发生异常时返回 null
        return null;
    }

   
}

日期工具类

package com.mdgyl.common.util;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * description: 日期工具类
 * author: wuyc
 * createTime: 2024-07-13 16:31:44
 */
public class DateUtils {

    public final static String YYYYMMDD = "yyyyMMdd";

    public final static String YYYY_MM_DD = "yyyy-MM-dd";

    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前是哪一年
     *
     * @return 当前年
     */
    public static int getLocalYear() {
        return LocalDateTime.now().getYear();
    }

    /**
     * 获取当前是哪一月
     *
     * @return 当前月
     */
    public static int getLocalMonth() {
        return LocalDateTime.now().getMonthValue();
    }

    /**
     * 获取今天是本周的第几天
     *
     * @return 本周第几天
     */
    public static int getWeekDay() {
        return LocalDateTime.now().getDayOfWeek().getValue();
    }

    /**
     * 获取今天是本月的第几天
     *
     * @return 本月第几天
     */
    public static int getMonthDay() {
        return LocalDateTime.now().getDayOfMonth();
    }

    /**
     * 获取今天是本年的第几天
     *
     * @return 本月第几天
     */
    public static int getYearDay() {
        return LocalDateTime.now().getDayOfYear();
    }

    /**
     * 获取当前日期
     *
     * @return yyyy-MM-dd
     */
    public static String getLocalDateStr() {
        return localDateTimeFormat(YYYY_MM_DD);
    }

    /**
     * 格式化日期为字符串
     *
     * @return yyyy-MM-dd
     */
    public static String getLocalDateTimeStr(LocalDateTime localDateTime) {
        return localDateTimeFormat(YYYY_MM_DD);
    }

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getLocalDateTimeStr() {
        return localDateTimeFormat(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 格式化当前日期
     *
     * @return 返回传入的时间格式
     */
    public static String localDateTimeFormat(String pattern) {
        return localDateTimeFormat(LocalDateTime.now(), pattern);
    }

    /**
     * 格式化日期
     *
     * @return 返回传入的时间格式
     */
    public static String localDateTimeFormat(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 格式化当前日期
     *
     * @return 返回传入的时间格式
     */
    public static LocalDateTime dataStrFormatLocalDate(String dateStr, String pattern) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }

        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(dateStr, formatter);
        } catch (Exception e) {
            return null;
        }
    }

}
自定义断言工具类
package com.mdgyl.common.util;

import com.jxdinfo.hussar.platform.core.base.apiresult.IResultCode;
import com.mdgyl.common.enums.CommonExceptionEnum;
import com.mdgyl.common.exception.BusinessException;
import com.mdgyl.common.exception.BusinessSystemException;
import com.mdgyl.common.exception.StandardResultCode;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Objects;

/**
 * description: 自定义断言工具类
 * author: wuyc
 * createTime: 2023-11-30 10:22:14
 */
public class AssertsUtils {

    /**
     * 字符串为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     */
    public static void isBlank(String param, String errorMsg) {
        isBlank(param, errorMsg, CommonExceptionEnum.SYSTEM_ERROR.getCode());
    }

    /**
     * 字符串为空抛出异常
     *
     * @param param    校验参数
     * @param resultCode 错误枚举
     */
    public static void isBlank(String param, IResultCode resultCode) {
        isBlank(param, resultCode.getMessage(), resultCode.getCode());
    }

    /**
     * 字符串为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     * @param code     错误编码
     */
    public static void isBlank(String param, String errorMsg, Integer code) {
        if (StringUtils.isEmpty(param)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 字符串不为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     */
    public static void isNotBlank(String param, String errorMsg) {
        isNotBlank(param, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 字符串不为空抛出异常
     *
     * @param param    校验参数
     * @param resultCode 错误枚举
     */
    public static void isNotBlank(String param, IResultCode resultCode) {
        if (!StringUtils.isEmpty(param)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 字符串不为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     * @param code     错误编码
     */
    public static void isNotBlank(String param, String errorMsg, Integer code) {
        if (!StringUtils.isEmpty(param)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 对象为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     */
    public static void isNull(Object param, String errorMsg) {
        isNull(param, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 对象为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     * @param code     错误编码
     */
    public static void isNull(Object param, String errorMsg, Integer code) {
        if (Objects.isNull(param)) {
            throw new BusinessSystemException(code,errorMsg);
        }
    }

    /**
     * 对象为空抛出异常
     *
     * @param param    校验参数
     * @param resultCode 错误枚举
     */
    public static void isNull(Object param, IResultCode resultCode) {
        if (Objects.isNull(param)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 对象不为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     */
    public static void nonNull(Object param, String errorMsg) {
        nonNull(param, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 对象不为空抛出异常
     *
     * @param param    校验参数
     * @param errorMsg 错误描述
     * @param code     错误编码
     */
    public static void nonNull(Object param, String errorMsg, Integer code) {
        if (Objects.nonNull(param)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 对象不为空抛出异常
     *
     * @param param    校验参数
     * @param resultCode 错误枚举
     */
    public static void nonNull(Object param, IResultCode resultCode) {
        if (Objects.nonNull(param)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 集合为空抛出异常
     *
     * @param collection 集合参数
     * @param errorMsg   错误描述
     */
    public static  void isEmpty(Collection collection, String errorMsg) {
        isEmpty(collection, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 集合为空抛出异常
     *
     * @param collection 集合参数
     * @param errorMsg   错误描述
     * @param code       错误编码
     */
    public static  void isEmpty(Collection collection, String errorMsg, Integer code) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 集合为空抛出异常
     *
     * @param collection 集合参数
     * @param resultCode   错误枚举
     */
    public static  void isEmpty(Collection collection, IResultCode resultCode) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 集合不为空抛出异常
     *
     * @param collection 集合参数
     * @param errorMsg   错误描述
     */
    public static  void isNotEmpty(Collection collection, String errorMsg) {
        isNotEmpty(collection, errorMsg, CommonExceptionEnum.SYSTEM_ERROR.getCode());
    }

    /**
     * 集合不为空抛出异常
     *
     * @param collection 集合参数
     * @param errorMsg   错误描述
     * @param code       错误编码
     */
    public static  void isNotEmpty(Collection collection, String errorMsg, Integer code) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 集合不为空抛出异常
     *
     * @param collection 集合参数
     * @param resultCode 错误枚举
     */
    public static  void isNotEmpty(Collection collection, IResultCode resultCode) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param errorMsg    错误描述
     */
    public static void isEqual(Object firstValue, Object secondValue, String errorMsg) {
        isEqual(firstValue, secondValue, errorMsg, CommonExceptionEnum.SYSTEM_ERROR.getCode());
    }

    /**
     * 相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param errorMsg    错误描述
     * @param code        错误编码
     */
    public static void isEqual(Object firstValue, Object secondValue, String errorMsg, Integer code) {
        if (Objects.equals(firstValue, secondValue)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param resultCode  错误枚举
     */
    public static void isEqual(Object firstValue, Object secondValue, IResultCode resultCode) {
        if (Objects.equals(firstValue, secondValue)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 不相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param errorMsg    错误描述
     */
    public static void isNotEqual(Object firstValue, Object secondValue, String errorMsg) {
        isNotEqual(firstValue, secondValue, errorMsg, CommonExceptionEnum.SYSTEM_ERROR.getCode());
    }

    /**
     * 不相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param errorMsg    错误描述
     * @param code        错误编码
     */
    public static void isNotEqual(Object firstValue, Object secondValue, String errorMsg, Integer code) {
        if (!Objects.equals(firstValue, secondValue)) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 不相等时抛出异常
     *
     * @param firstValue  第一个值
     * @param secondValue 第二个值
     * @param resultCode  错误枚举
     */
    public static void isNotEqual(Object firstValue, Object secondValue, IResultCode resultCode) {
        if (!Objects.equals(firstValue, secondValue)) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }


    /**
     * 为true时抛出异常
     *
     * @param expression 条件true|false
     * @param errorMsg   错误描述
     */
    public static void isTrue(Boolean expression, String errorMsg) {
        isTrue(expression, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 为true时抛出异常
     *
     * @param expression 条件true|false
     * @param errorMsg   错误描述
     * @param code       错误编码
     */
    public static void isTrue(Boolean expression, String errorMsg, Integer code) {
        if (expression) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 为true时抛出异常
     *
     * @param expression 条件true|false
     * @param resultCode 错误枚举
     */
    public static void isTrue(Boolean expression, IResultCode resultCode) {
        if (expression) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

    /**
     * 为false时抛出异常
     *
     * @param expression 条件true|false
     * @param errorMsg   错误描述
     */
    public static void isFalse(Boolean expression, String errorMsg) {
        isFalse(expression, errorMsg, StandardResultCode.SYSTEM_EXCEPTION.getCode());
    }

    /**
     * 为false时抛出异常
     *
     * @param expression 条件true|false
     * @param errorMsg   错误描述
     * @param code       错误编码
     */
    public static void isFalse(Boolean expression, String errorMsg, Integer code) {
        if (!expression) {
            throw new BusinessSystemException(code, errorMsg);
        }
    }

    /**
     * 为false时抛出异常
     *
     * @param expression 条件true|false
     * @param resultCode   错误枚举
     */
    public static void isFalse(Boolean expression, IResultCode resultCode) {
        if (!expression) {
            throw new BusinessSystemException(resultCode.getCode(), resultCode.getMessage());
        }
    }

}

List集合相关工具类

package com.mdgyl.common.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * description: java8 List相关工具类
 * author: wuyc
 * createTime: 2023-12-20 18:33:13
 */
public class ListUtils {

    private static final int ZERO = 0;

    /**
     * 根据条件获取集合里符合条件第一条数据
     *
     * @param dataList  入参集合
     * @param predicate 条件
     * @return T
     */
    public static  T filterFirst(List dataList,
                                    Predicate predicate) {
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }

        return dataList.stream()
                .filter(predicate)
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据条件获取集合里符合条件第一条数据,并返回相应类型
     *
     * @param dataList  入参集合
     * @param predicate 条件
     * @param clazz     强转类型
     * @return K
     */
    public static  K filterFirst(List dataList,
                                       Predicate predicate, Class clazz) {
        return CollectionUtils.isEmpty(dataList) ? null : clazz.cast(filterFirst(dataList, predicate));
    }


    /**
     * 根据条件获取集合里符合条件第一条数据
     *
     * @param dataList  入参集合
     * @param function  转为map - key属性的function函数
     * @param predicate 条件
     * @return
     */
    public static  R filterMapFirst(List dataList,
                                          Function function,
                                          Predicate predicate) {
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }

        return dataList.stream()
                .map(function)
                .filter(predicate)
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据条件过滤集合,返回新的集合
     *
     * @param dataList  入参集合
     * @param predicate 条件
     * @return List
     */
    public static  List filterList(List dataList,
                                         Predicate predicate) {
        return filterMapToList(dataList, Function.identity(), predicate);
    }

    /**
     * 将集合转换为map, key为对象指定列 value为当前对象
     *
     * @param dataList 入参集合
     * @param function 转为map - key属性的function函数
     * @return Map
     */
    public static  List mapToList(List dataList,
                                           Function function) {
        return filterMapToList(dataList, function, data -> true);
    }

    /**
     * 条件过滤且获取集合里某一列的值
     *
     * @param dataList  入参集合
     * @param function  获取的某一列
     * @param condition 条件
     * @return List
     */
    public static  List filterMapToList(List dataList,
                                                 Function function,
                                                 Predicate condition) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Lists.newArrayList();
        }

        return dataList.stream()
                .filter(condition)
                .map(function)
                .collect(Collectors.toList());
    }

    /**
     * 获取集合里某一列的值且去重
     *
     * @param dataList 入参集合
     * @param function 获取的某一列
     * @return List
     */
    public static  List distinctMapToList(List dataList,
                                                   Function function) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Lists.newArrayList();
        }

        return dataList.stream()
                .map(function)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 将集合转换为map, key为对象指定列 value为当前对象
     *
     * @param dataList 入参集合
     * @param function 转为map - key属性的function函数
     * @return Map
     */
    public static  Map toMap(List dataList,
                                         Function function) {
        return toMap(dataList, function, Function.identity());
    }

    /**
     * 将集合转换为map, key和value均为对象指定列
     *
     * @param dataList      入参集合
     * @param functionKey   转为map - key属性的function函数
     * @param functionValue 转为map - value属性的function函数
     * @return Map
     */
    public static  Map toMap(List dataList,
                                            Function functionKey,
                                            Function functionValue) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Maps.newHashMap();
        }

        return dataList.stream()
                .collect(Collectors.toMap(functionKey, functionValue, (v1, v2) -> v1));
    }

    /**
     * 根据条件过滤集合后,将集合转换为map, key为对象指定列 value为当前对象
     *
     * @param dataList        集合数据
     * @param function        转为map - key属性的function函数
     * @param filterCondition 过滤条件
     * @return Map
     */
    public static  Map filterToMap(List dataList,
                                               Function function,
                                               Predicate filterCondition) {
        return filterToMap(dataList, function, Function.identity(), filterCondition);
    }

    /**
     * 根据条件过滤集合后,将集合转换为map, key和value均为对象指定列
     *
     * @param dataList        集合数据
     * @param functionKey     转为map - key属性的function函数
     * @param functionValue   转为map - value属性的function函数
     * @param filterCondition 过滤条件
     * @return Map
     */
    public static  Map filterToMap(List dataList,
                                                  Function functionKey,
                                                  Function functionValue,
                                                  Predicate filterCondition) {
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }

        return dataList.stream()
                .filter(filterCondition)
                .collect(Collectors.toMap(functionKey, functionValue, (v1, v2) -> v1));
    }

    /**
     * 获取集合和集合里的集合合并成一个集合
     *
     * @param dataList 集合数据
     * @param function flatMap入参函数
     * @return 去重后的List集合
     */
    public static  List flatMapToList(Collection> dataList,
                                            Function, Stream> function) {
        return flatMapColumnToList(dataList, Function.identity(), function);
    }

    /**
     * 获取集合和集合里的集合合并成一个集合, 并且获取某一列
     *
     * @param dataList        集合数据
     * @param mapFunction     转换为列的map函数
     * @param flatMapFunction flatMap入参函数
     * @return 去重后某一列的List集合
     */
    public static  List flatMapColumnToList(Collection> dataList,
                                                     Function mapFunction,
                                                     Function, Stream> flatMapFunction) {
        return filterFlatMapColumnToList(dataList, mapFunction, flatMapFunction, data -> true);
    }

    /**
     * 获取集合和集合里的集合合并成一个集合
     *
     * @param dataList 集合数据
     * @param function flatMap入参函数
     * @return 去重后的List集合
     */
    public static  List filterFlatMapToList(Collection> dataList,
                                                  Function, Stream> function,
                                                  Predicate filterCondition) {
        return filterFlatMapColumnToList(dataList, Function.identity(), function, filterCondition);
    }

    /**
     * 获取集合和集合里的集合合并成一个集合, 并且获取某一列
     *
     * @param dataList        集合数据
     * @param mapFunction     转换为列的map函数
     * @param flatMapFunction flatMap入参函数
     * @return 去重后某一列的List集合
     */
    public static  List filterFlatMapColumnToList(Collection> dataList,
                                                           Function mapFunction,
                                                           Function, Stream> flatMapFunction,
                                                           Predicate filterCondition) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Lists.newArrayList();
        }

        return dataList.stream()
                .flatMap(flatMapFunction)
                .filter(filterCondition)
                .map(mapFunction)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 将集合和集合里的集合转化成map, key为对象指定列, value为对象本身
     *
     * @param dataList        集合数据
     * @param keyFunction     转为map - key属性的function函数
     * @param flatMapFunction flatMap入参函数
     * @return Map
     */
    public static  Map flatMapToMap(Collection> dataList,
                                                Function keyFunction,
                                                Function, Stream> flatMapFunction) {
        return filterFlatMapToMap(dataList, keyFunction, flatMapFunction, data -> true);
    }

    /**
     * 将集合和集合里的集合转化成map, key、value均为对象指定列
     *
     * @param dataList        集合数据
     * @param keyFunction     转为map - key属性的function函数
     * @param valueFunction   转为map - value属性的function函数
     * @param flatMapFunction flatMap入参函数
     * @return Map
     */
    public static  Map flatMapToMap(List> dataList,
                                                   Function keyFunction,
                                                   Function valueFunction,
                                                   Function, Stream> flatMapFunction) {
        return filterFlatMapToMap(dataList, keyFunction, valueFunction, flatMapFunction, data -> true);
    }

    /**
     * 将集合和集合里的集合转化成map, key为对象指定列, value为对象本身
     *
     * @param dataList        集合数据
     * @param keyFunction     转为map - key属性的function函数
     * @param flatMapFunction flatMap入参函数
     * @param filterCondition 过滤条件
     * @return Map
     */
    public static  Map filterFlatMapToMap(Collection> dataList,
                                                      Function keyFunction,
                                                      Function, Stream> flatMapFunction,
                                                      Predicate filterCondition) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Maps.newHashMap();
        }

        return dataList.stream()
                .flatMap(flatMapFunction)
                .filter(filterCondition)
                .distinct()
                .collect(Collectors.toMap(keyFunction, Function.identity(), (v1, v2) -> v1));
    }

    /**
     * 将集合和集合里的集合转化成map, key为对象指定列, value为对象本身
     *
     * @param dataList        集合数据
     * @param keyFunction     转为map - key属性的function函数
     * @param valueFunction   转为map - value属性的function函数
     * @param flatMapFunction flatMap入参函数
     * @return Map
     */
    public static  Map filterFlatMapToMap(List> dataList,
                                                         Function keyFunction,
                                                         Function valueFunction,
                                                         Function, Stream> flatMapFunction,
                                                         Predicate filterCondition) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Maps.newHashMap();
        }

        return dataList.stream()
                .flatMap(flatMapFunction)
                .filter(filterCondition)
                .distinct()
                .collect(Collectors.toMap(keyFunction, valueFunction, (v1, v2) -> v1));
    }

    /**
     * 根据对象属性进行分组
     *
     * @param dataList      入参数据
     * @param groupFunction 进行分组的属性
     * @return Map>
     */
    public static  Map> group(List dataList,
                                               Function groupFunction) {
        return filterGroup(dataList, Objects::nonNull, groupFunction);
    }

    /**
     * 根据条件过滤后,根据对象属性进行分组
     *
     * @param dataList        入参数据
     * @param filterPredicate 过滤条件
     * @param groupFunction   对象熟悉
     * @return Map>
     */
    public static  Map> filterGroup(List dataList,
                                                     Predicate filterPredicate,
                                                     Function groupFunction) {
        if (CollectionUtils.isEmpty(dataList)) {
            return Maps.newHashMap();
        }
        return dataList.stream()
                .filter(filterPredicate)
                .collect(Collectors.groupingBy(groupFunction));
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList 集合参数
     * @param function 求和的属性值
     * @return int
     */
    public static  int mapToInt(List dataList,
                                   ToIntFunction function) {
        return filterMapToInt(dataList, function, Objects::nonNull);
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList        集合参数
     * @param function        求和的属性值
     * @param filterPredicate 过滤条件
     * @return int
     */
    public static  int filterMapToInt(List dataList,
                                         ToIntFunction function,
                                         Predicate filterPredicate) {
        if (CollectionUtils.isEmpty(dataList)) {
            return ZERO;
        }
        return dataList.stream()
                .filter(filterPredicate)
                .mapToInt(function).sum();
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList 集合参数
     * @param function 求和的属性值
     * @return long
     */
    public static  long mapToLong(List dataList,
                                     ToLongFunction function) {
        return filterMapToLong(dataList, function, Objects::nonNull);
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList        集合参数
     * @param function        求和的属性值
     * @param filterPredicate 过滤条件
     * @return long
     */
    public static  long filterMapToLong(List dataList,
                                           ToLongFunction function,
                                           Predicate filterPredicate) {
        if (CollectionUtils.isEmpty(dataList)) {
            return ZERO;
        }
        return dataList.stream()
                .filter(filterPredicate)
                .mapToLong(function).sum();
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList 集合参数
     * @param function 求和的属性值
     * @return double
     */
    public static  Double mapToDouble(List dataList,
                                         ToDoubleFunction function) {
        return filterMapToDouble(dataList, function, Objects::nonNull);
    }

    /**
     * 获取List集合里某一列的总和
     *
     * @param dataList        集合参数
     * @param function        求和的属性值
     * @param filterPredicate 过滤条件
     * @return long
     */
    public static  double filterMapToDouble(List dataList,
                                               ToDoubleFunction function,
                                               Predicate filterPredicate) {
        if (CollectionUtils.isEmpty(dataList)) {
            return ZERO;
        }
        return dataList.stream()
                .filter(filterPredicate)
                .mapToDouble(function).sum();
    }

    /**
     * 对集合排序
     *
     * @param dataList   入参集合
     * @param comparator 排序参数
     * @return 排序好的集合
     */
    public static  List sortedList(List dataList, Comparator comparator) {
        if (org.springframework.util.CollectionUtils.isEmpty(dataList)) {
            return Lists.newArrayList();
        }
        return dataList.stream().sorted(comparator).collect(Collectors.toList());
    }

    /**
     * 将字符串根据特定字符分割后,转换成特定类型集合
     *
     * @param dataStr     字符串入参
     * @param regex       分割符号
     * @param mapFunction 转换后类型函数
     * @return List
     */
    public static  List stringToList(String dataStr,
                                           String regex,
                                           Function mapFunction) {
        if (Objects.isNull(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  String listToString(List dataList,
                                          String regex) {
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        return dataList.stream().map(String::valueOf)
                .collect(Collectors.joining(regex));
    }

    public static  int getCollectionSize(List dataList) {
        return CollectionUtils.isEmpty(dataList) ? ZERO : dataList.size();
    }


}

你可能感兴趣的:(java,开发语言)