java數(shù)據(jù)處理工具類

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.fw.common.constant.CommonConstant.Symbol.COMMA;
import static cn.fw.common.constant.CommonConstant.Symbol.EMPTY;

/**
 * 公共幫助服務(wù)
 * <p>
 * create at 2020-05-27
 */
public interface CommonSupport {
    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R1, R2> Map<R1, List<R2>> transfersMap(Collection<P> coll, Function<P, R1> column1, Function<P, R2> column2) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1) || Objects.isNull(column2)) {
            return Collections.emptyMap();
        }
        return coll.stream().collect(Collectors.toMap(column1,
            i -> new ArrayList<>(Collections.singletonList(column2.apply(i))), (i1, i2) -> {
                i1.addAll(i2);
                return new ArrayList<>();
            }));
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll   集合對(duì)象
     * @param column 列
     * @return 返回鍵值對(duì)
     */
    default <P, R> Map<R, List<P>> transfersMap(Collection<P> coll, Function<P, R> column) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column)) {
            return Collections.emptyMap();
        }
        return coll.stream().collect(Collectors.groupingBy(column));
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll   集合對(duì)象
     * @param column 列
     * @return 返回鍵值對(duì)
     */
    default <P, R> Map<R, P> transferMap(Collection<P> coll, Function<P, R> column) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column)) {
            return Collections.emptyMap();
        }
        return coll.stream().collect(Collectors.toMap(column, i -> i, (i1, i2) -> i1));
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R1, R2, R3> Map<R1, Map<R2, R3>> transferMap(Collection<P> coll, Function<P, R1> column1, Function<P, R2> column2, Function<P, R3> column3) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1) || Objects.isNull(column2) || Objects.isNull(column3)) {
            return Collections.emptyMap();
        }
        final Map<R1, Map<R2, R3>> map = new HashMap<>();
        coll.stream().collect(Collectors.groupingBy(column1)).forEach((k, v) -> map.put(k, transferMap(v, column2, column3)));
        return map;
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R1, R2> Map<R1, Map<R2, P>> transferToMap(Collection<P> coll, Function<P, R1> column1, Function<P, R2> column2) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1) || Objects.isNull(column2)) {
            return Collections.emptyMap();
        }
        final Map<R1, Map<R2, P>> map = new HashMap<>();
        coll.stream().collect(Collectors.groupingBy(column1)).forEach((k, v) -> map.put(k,
            v.stream().collect(Collectors.toMap(column2, i -> i, (i1, i2) -> i1))));
        return map;
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R, R1, R2, R3> Map<R1, Map<R2, R3>> transferToMap(Collection<P> coll, Function<P, List<R>> column, Function<P, R1> column1, Function<R, R2> column2, Function<R, R3> column3) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column) || Objects.isNull(column1) || Objects.isNull(column2) || Objects.isNull(column3)) {
            return Collections.emptyMap();
        }
        final Map<R1, Map<R2, R3>> map = new HashMap<>();
        coll.stream().collect(Collectors.groupingBy(column1)).forEach((k, v) -> map.put(k,
            v.stream().map(column).flatMap(Collection::parallelStream)
                .collect(Collectors.toMap(column2, column3, (i1, i2) -> i1))));
        return map;
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R1, R2> Map<R1, R2> transferMap(Collection<P> coll, Function<P, R1> column1, Function<P, R2> column2) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1) || Objects.isNull(column2)) {
            return Collections.emptyMap();
        }
        return coll.stream().collect(Collectors.toMap(column1, column2, (i1, i2) -> i1));
    }

    /**
     * 將集合制定字段轉(zhuǎn)Map鍵值對(duì)
     *
     * @param coll    集合對(duì)象
     * @param column1 列1
     * @param column2 列2
     * @return 返回鍵值對(duì)
     */
    default <P, R1, R2> Map<R1, R2> transferMap(Collection<P> coll, Function<P, R1> column1, Function<P, R2> column2, BinaryOperator<R2> mergeFunction) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1) || Objects.isNull(column2)) {
            return Collections.emptyMap();
        }
        return coll.stream().collect(Collectors.toMap(column1, column2, mergeFunction));
    }

    /**
     * 將ID字符串轉(zhuǎn)換為id集合
     *
     * @param ids ID集合字符串","分割
     * @return id集合
     */
    default List<Long> strToIds(String ids) {
        if (StringUtils.isBlank(ids)) {
            return Collections.emptyList();
        }
        return Arrays.stream(ids.split(COMMA))
            .filter(StringUtils::isNotBlank)
            .map(Long::valueOf)
            .distinct()
            .collect(Collectors.toList());
    }

    /**
     * 將ID字符串轉(zhuǎn)換為id集合
     *
     * @param ids ID集合字符串","分割
     * @return id集合
     */
    default List<String> strToStrs(String ids) {
        if (StringUtils.isBlank(ids)) {
            return Collections.emptyList();
        }
        return Arrays.stream(ids.split(COMMA))
            .filter(StringUtils::isNotBlank)
            .distinct()
            .collect(Collectors.toList());
    }

    /**
     * 將列表固定字段ID字符串轉(zhuǎn)換為id集合
     *
     * @param coll 對(duì)象集合
     * @return id集合
     */
    default <P> List<Long> strToIds(Collection<P> coll, Function<P, String> column) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptyList();
        }
        return coll.stream().map(column)
            .filter(StringUtils::isNotBlank)
            .map(this::strToIds)
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
    }

    /**
     * 將列表ID集合和ID名稱鍵值對(duì)組裝名稱集合
     *
     * @param ids ID集合
     * @param map ID名稱鍵值對(duì)
     * @return 名稱
     */
    default String idsToNames(List<Long> ids, Map<Long, String> map) {
        if (CollectionUtils.isEmpty(ids) || CollectionUtils.isEmpty(map)) {
            return EMPTY;
        }
        return ids.stream().distinct()
            .map(i -> map.getOrDefault(i, EMPTY))
            .filter(StringUtils::isNotBlank)
            .distinct()
            .collect(Collectors.joining(COMMA));
    }

    /**
     * 獲取集合制定字段集合
     *
     * @param coll 集合
     * @return 字段集合
     */
    default <P, R> List<R> collIds(Collection<P> coll, Function<P, R> column) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptyList();
        }
        return coll.stream()
            .map(column)
            .distinct()
            .collect(Collectors.toList());
    }

    /**
     * 根據(jù)條件過濾集合數(shù)據(jù)
     *
     * @param coll 集合
     * @return 過濾后集合
     */
    default <P> List<P> filter(Collection<P> coll, Predicate<P> column) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptyList();
        }
        return coll.stream()
            .filter(column)
            .distinct()
            .collect(Collectors.toList());
    }

    /**
     * 獲取集合制定字段集合
     *
     * @param coll 集合
     * @return 字段集合
     */
    default <P, R> List<R> colls(Collection<P> coll, Function<P, List<R>> column) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptyList();
        }
        return coll.stream()
            .map(column)
            .flatMap(Collection::parallelStream)
            .collect(Collectors.toList());
    }

    /**
     * 獲取集合制定字段集合
     *
     * @param coll 集合
     * @return 字段集合
     */
    default <P, R> Set<R> toSets(Collection<P> coll, Function<P, R> column) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptySet();
        }
        return coll.stream()
            .map(column)
            .collect(Collectors.toSet());
    }

    /**
     * 獲取集合制定字段集合
     *
     * @param coll 集合
     * @return 字段集合
     */
    default <P, R1, R2> List<R2> collIds(Collection<P> coll, Function<P, List<R1>> column1, Function<R1, R2> column2) {
        if (CollectionUtils.isEmpty(coll)) {
            return Collections.emptyList();
        }
        return coll.stream()
            .map(column1)
            .flatMap(Collection::parallelStream)
            .map(column2)
            .collect(Collectors.toList());
    }

    /**
     * 將將ID集合轉(zhuǎn)字符串
     *
     * @param coll 集合
     * @return 字段集合
     */
    default String idsToStr(Collection<Long> coll) {
        if (CollectionUtils.isEmpty(coll)) {
            return EMPTY;
        }
        return coll.stream()
            .map(String::valueOf)
            .distinct()
            .collect(Collectors.joining(COMMA));
    }

    /**
     * 將將ID集合轉(zhuǎn)字符串
     *
     * @param coll 集合
     * @return 字段集合
     */
    default String collToStr(Collection<String> coll) {
        if (CollectionUtils.isEmpty(coll)) {
            return EMPTY;
        }
        return coll.stream()
            .map(String::valueOf)
            .distinct()
            .collect(Collectors.joining(COMMA));
    }

    /**
     * 將將ID集合轉(zhuǎn)字符串
     *
     * @param coll 集合
     * @return 字段集合
     */
    default String collIdToStr(Collection<Long> coll) {
        if (CollectionUtils.isEmpty(coll)) {
            return EMPTY;
        }
        return coll.stream()
            .map(String::valueOf)
            .distinct()
            .collect(Collectors.joining(COMMA));
    }

    /**
     * 指定字段求和
     *
     * @param coll   集合
     * @param column 指定列
     * @return 求和值
     */
    default <P> Double sum(Collection<P> coll, Function<P, Number> column) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column)) {
            return 0d;
        }
        return coll.stream().map(column).mapToDouble(Number::doubleValue).sum();
    }

    /**
     * 列表指定字段求和
     *
     * @param coll    集合
     * @param column1 指定列
     * @return 求和后列表
     */
    default <P, R1> List<P> collsColumnSum(Collection<P> coll, Function<P, R1> column1, Function<List<P>, P> column2) {
        if (CollectionUtils.isEmpty(coll) || Objects.isNull(column1)) {
            return Collections.emptyList();
        }
        return coll.stream()
            .collect(Collectors.groupingBy(column1))
            .values()
            .stream().map(column2)
            .collect(Collectors.toList());
    }

    /**
     * 列表匯總
     *
     * @param lists 可變參數(shù)列表
     * @return 匯總后的列表
     */
    default <P> List<P> collSummary(List<P>... lists) {
        Stream<P> stream = Stream.empty();
        for (List<P> values : lists) {
            stream = Stream.concat(stream, values.stream());
        }
        return stream.collect(Collectors.toList());
    }

    /**
     * 時(shí)間格式化
     *
     * @param date 時(shí)間
     * @param format 格式化方式
     * @return 格式化后的字符串
     */
    default String formatDate(Date date, String format) {
        if (StringUtils.isBlank(format) || Objects.isNull(date)) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 一天的開始時(shí)間
     *
     * @param date 時(shí)間
     * @return 格式化后的時(shí)間
     */
    default Date dateOfStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter1.parse(String.format("%s 00:00:00", formatter.format(date)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 一天的結(jié)束時(shí)間
     *
     * @param date 時(shí)間
     * @return 格式化后的時(shí)間
     */
    default Date dateOfEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter1.parse(String.format("%s 23:59:59", formatter.format(date)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容